Use an Image for the Background in Tkinter

Dev Prakash Sharma
Updated on 16-Apr-2021 07:06:46

3K+ Views

Background images in tkinter are versatile as the functionality can be used in making 3D, 2D games, screensaver, desktop visualizations software, etc. Tkinter canvas is used to work with all these functionalities in an application.ExampleIn this example, we will add a background image using the create_image() method in the canvas widget.#Import the required library from tkinter import * from PIL import Image, ImageTk from tkinter import ttk #Create an instance of tkinter window win= Tk() #Define the geometry of the window win.geometry("750x650") #Load the image bg= ImageTk.PhotoImage(file="./tutorialspoint.png") #Create a canvas canvas= Canvas(win, width= 400, height= 200) canvas.pack(expand=True, fill= BOTH) #Add the ... Read More

Use a Custom Font in Tkinter

Dev Prakash Sharma
Updated on 16-Apr-2021 07:05:03

2K+ Views

To define and display a custom font in Python tkinter, we generally use an inbuilt font library defined in tkinter. In order to import the tkinter Font library in the notebook, type the following in the shell, from tkinter.font import FontNow, create an Object of Font using the Font(..options) function and define other properties of the font such as font-family, size, weight, slant, underline, strike, etc.Example#Import the required library from tkinter import * #Create an instance of tkinter frame win = Tk() win.geometry("750x250") #Create a String Object and set the default value var = StringVar() #Create a text label label ... Read More

Update Image of a Tkinter Label Widget

Dev Prakash Sharma
Updated on 16-Apr-2021 07:03:16

6K+ Views

We have used Label widget to group all the widgets in the application. A Label widget takes text and images in the constructor that sets the label with the position in the top-left corner of the window. However, to change or update the image associated with the Label, we can use a callable method where we provide the information of other images.ExampleIn the following example, we will create a button to update the Label image.#Import the required library from tkinter import* from PIL import Image, ImageTk #Create an instance of tkinter frame win= Tk() #Define geometry of the ... Read More

Update Image in a Tkinter Canvas

Dev Prakash Sharma
Updated on 16-Apr-2021 07:00:49

12K+ Views

Canvas can be used to play with images, animating objects, 3D modeling, displaying text, and many more. Moreover, we can display an image file using the create_image() constructor.Following this, let us build an application that can update the canvas images locally. We can add a button to trigger the event which when pressed will change the canvas image.To change a particular image, we can configure the canvas by using the itemconfig() constructor. It takes image files which need to be updated and displayed them on the window.Use three images of your choice and save them in the same project directory.Example#Import ... Read More

Set Focus on Entry Widget in Tkinter

Dev Prakash Sharma
Updated on 16-Apr-2021 06:58:25

8K+ Views

Let us suppose that there are some widgets present in an application such that we have to focus on a particular widget. By using the focus_set() method, we can activate the focus on any widget and give them priority while executing the application.Example#Import tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry of tkinter window win.geometry("750x250") #Create an Entry Widget entry= Entry(win, width= 25) entry.insert(0, "ttk Entry widget") entry.pack() #Set the focus to Entry widget entry.focus_set() win.mainloop()OutputWhen we run our program, it will display a window with ... Read More

Position Tkinter Widgets for Better Layout

Dev Prakash Sharma
Updated on 16-Apr-2021 06:54:51

383 Views

In a tkinter GUI skeleton, we can add any number of widgets to make it more functional and operational. However, sometimes, it seems difficult to resize and position the widgets so that they don’t stick together and are non-variable in size. We can add padding using the widget pack manager to make every widget stand as one separate entity.Example#Import the required library from tkinter import* from tkinter import ttk #Create an instance of tkinter frame win= Tk() #Set the geometry win.geometry("750x250") #Create an Label Widget Label(win, text= "New Line Text", font= ('Helvetica 15 underline')).pack(pady=20) left=Button(win, text="Button1", font= ... Read More

Interactively Validate Entry Widget Content in Tkinter

Dev Prakash Sharma
Updated on 16-Apr-2021 06:52:46

2K+ Views

Validating the content is a necessary part of any featured application where we allow only the required data to be processed. An Entry Widget in tkinter is used to display single line text Input. However, we can validate the Entry widget to accept only digits or alphabets.Let us first create an Entry widget that accepts only digits input. So initially, we will create an Entry widget and using register(callback) function, we will call to validate the Entry widget which validates whenever a key is stroked. It returns a string that can be used to call a function. Then, calling the ... Read More

Highlight Text in a Tkinter Text Widget

Dev Prakash Sharma
Updated on 16-Apr-2021 06:50:53

6K+ Views

Tkinter Text widget is used to create text fields that accept multiline user inputs. Let us suppose that in a text widget, we want to highlight some text. In order to highlight a specific text written in the text widget, tkinter provides the tag_add(tag, i, j) method. It adds tags to the specific text by defining the indexes, i and j.ExampleIn this example, we will create a window application that contains some text and a button which can be triggered to highlight the text.#Import tkinter library from tkinter import * #Create an instance of tkinter frame win= Tk() win.geometry("750x450") #Define a ... Read More

Disable Grey Out of a Checkbutton in Tkinter

Dev Prakash Sharma
Updated on 15-Apr-2021 14:35:59

1K+ Views

Tkinter provides a variety of input widgets such as entry widget, text widget, listbox, combobox, spinbox, checkbox, etc. Checkboxes are used for taking validity input and the state gets active whenever the user clicks on the checkbutton. In terms of a particular application, we can enable and disable the state of CheckButtons by using the state property.Example#Import the required library from tkinter import* from tkinter import ttk #Create an instance of tkinter frame win= Tk() #Set the geometry win.geometry("750x250") #Create CheckButtons chk= ttk.Checkbutton(win, text="Python") chk.pack() chk.config(state=DISABLED) win.mainloop()OutputRunning the example code will display a window with a check button that is ... Read More

Disable and Enable a Button in Tkinter

Dev Prakash Sharma
Updated on 15-Apr-2021 14:34:12

19K+ Views

There are various attributes and properties in each tkinter widget to help us extend the functionality of the application. Tkinter Button widgets can be enabled and disabled by defining its state in the Button Object. The state attribute generally accepts two values Normal and Disabled which are used for enabling and disabling the button, respectively.Example#Import necessary Library from tkinter import * from tkinter import ttk from tkinter.filedialog import asksaveasfile #Create an instance of tkinter window win= Tk() #Set the geometry of tkinter window win.geometry("750x250") #Define the function to change the value in label widget def change_text(label):    label.configure(text= "Hey, I ... Read More

Advertisements