Found 310 Articles for GUI-Programming

How to bind the Enter key to a tkinter window?

Dev Prakash Sharma
Updated on 05-Aug-2021 13:57:57

18K+ Views

Tkinter events are executed at runtime and when we bind these events with a button or key, then we will get access to prioritize the event in the application.To bind the key with an event in Tkinter window, we can use bind('', callback) by specifying the key and the callback function as the arguments. Once we bind the key to an event, we can get full control over the events.Example# Import the required libraries from tkinter import * from PIL import Image, ImageTk # Create an instance of tkinter frame or window win=Tk() # Set the size ... Read More

How to draw a png image on a Python tkinter canvas?

Dev Prakash Sharma
Updated on 05-Aug-2021 13:56:10

3K+ Views

To work with images in tkinter, Python provides PIL or Pillow toolkit. It has many built-in functions that can be used to operate an image of different formats.To open an image in a canvas widget, we have use create_image(x, y, image, **options) constructor. When we pass the Image value to the constructor, it will display the image in the canvas.Example# Import the required libraries from tkinter import * from PIL import Image, ImageTk # Create an instance of tkinter frame or window win=Tk() # Set the size of the window win.geometry("700x600") # Create a canvas widget canvas=Canvas(win, ... Read More

How to set the font size of Entry widget in Tkinter?

Dev Prakash Sharma
Updated on 05-Aug-2021 13:54:30

4K+ Views

The Entry widget in tkinter is a basic one-line character Entry box that accepts single line user input. To configure the properties of the Entry widget such as its font-size and width, we can define an inline widget constructor.ExampleHere is an example of how you can define the font-size of the Entry widget.# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win=Tk() # Set the size of the window win.geometry("700x350") # Create an Entry widget entry=Entry(win, width=35, font=('Georgia 20')) entry.pack() win.mainloop()OutputRun the above ... Read More

How to change the width of a Frame dynamically in Tkinter?

Dev Prakash Sharma
Updated on 05-Aug-2021 13:53:31

19K+ Views

The frame widget in Tkinter works like a container where we can place widgets and all the other GUI components. To change the frame width dynamically, we can use the configure() method and define the width property in it.ExampleIn this example, we have created a button that is packed inside the main window and whenever we click the button, it will update the width of the frame.# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win=Tk() # Set the size of the window win.geometry("700x350") ... Read More

How to dynamically add/remove/update labels in a Tkinter window?

Dev Prakash Sharma
Updated on 05-Aug-2021 13:51:35

13K+ Views

We can use the Tkinter Label widget to display text and images. By configuring the label widget, we can dynamically change the text, images, and other properties of the widget.To dynamically update the Label widget, we can use either config(**options) or an inline configuration method such as for updating the text, we can use Label["text"]=text; for removing the label widget, we can use pack_forget() method.Example# Import the required libraries from tkinter import * from tkinter import ttk from PIL import ImageTk, Image # Create an instance of tkinter frame or window win=Tk() # Set the size of the ... Read More

How to draw a scale that ranges from red to green in Tkinter?

Dev Prakash Sharma
Updated on 05-Aug-2021 13:49:31

1K+ Views

A color gradient defines the range of position-dependent colors. To be more specific, if you want to create a rectangular scale in an application that contains some color ranges in it (gradient), then we can follow these steps −Create a rectangle with a canvas widget and define its width and height.Define a function to fill the color in the range. To fill the color, we can use hex values inside a tuple.Iterate over the range of the color and fill the rectangle with it.Example# Import the required libraries from tkinter import * from tkinter import ttk # Create an ... Read More

How to get radiobutton output in Tkinter?

Dev Prakash Sharma
Updated on 05-Aug-2021 13:47:19

9K+ Views

The radiobutton widget in Tkinter allows the user to make a selection for only one option from a set of given choices. The radiobutton has only two values, either True or False.If we want to get the output to check which option the user has selected, then we can use the get() method. It returns the object that is defined as the variable. We can display the selection in a label widget by casting the integer value in a string object and pass it in the text attributes.Example# Import the required libraries from tkinter import * from tkinter import ttk # ... Read More

Deleting a Label in Python Tkinter

Dev Prakash Sharma
Updated on 19-Jun-2021 08:47:30

19K+ Views

Tkinter label widgets are used to display text and images in the application. We can also configure the properties of Label widget that are created by default in a tkinter application.If we want to delete a label that is defined in a tkinter application, then we have to use the destroy() method.ExampleIn this example, we will create a Button that will allow the user to delete the label from the widget.# Import the required libraries from tkinter import * from tkinter import ttk from PIL import Image, ImageTk # Create an instance of tkinter frame or window win = ... Read More

How to hide and show canvas items on Tkinter?

Dev Prakash Sharma
Updated on 19-Jun-2021 08:34:29

3K+ Views

The Canvas widget is one of the versatile widgets in Tkinter. It is used in many applications for designing the graphical user interface such as designing, adding images, creating graphics, etc. We can add widgets in the Canvas widget itself. The widgets that lie inside the canvas are sometimes called "Canvas Items".If we want to show or hide the canvas items through a Button, then this can be achieved by using the "state" property in itemconfig(id, state) method.ExampleIn this example, we will add an image in the canvas and a button will be used to show/hide the image in the ... Read More

How do I get the index of an item in Tkinter.Listbox?

Dev Prakash Sharma
Updated on 19-Jun-2021 08:34:09

4K+ Views

We use the Tkinter Listbox widget to create a list of items. Each item in the listbox has some indexes that are assigned to them sequentially in vertical order.Let us suppose that we want to get the index of a clicked item in the listbox. Then, we have to first create a button that will capture the current selection of the items using list.curselection() method and then, we will print the index using the get() method.Example# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win = Tk() # Set the size ... Read More

Advertisements