- Tkinter Label
- Introduction to Tkinter Label widget
- Displaying a regular label
- Setting a specific font for the Label
- Displaying an image
- Summary
- Change the Tkinter Label Font Size
- Change the Tkinter Label Font Size
- Change the Tkinter Label Font Family
- Related Article — Tkinter Label
- Related Article — Tkinter Font
- How to set the font size in Tkinter?
- Method 1: Changing Tkinter font size using the font as a tuple
- Method 2: Changing tkinter font size using the font as an object
- Method 3: Using changing fonts using a custom class
- Summary
Tkinter Label
Summary: in this tutorial, you’ll learn about Tkinter Label widget and how to use it to display a text or image on the screen.
Introduction to Tkinter Label widget
Tkinter Label widget is used to display a text or image on the screen. To use a Label widget, you use the following general syntax:
label = ttk.Label(container, **options)
Code language: Python (python)
The Label widget has many options that allow you to customize its appearance:
Options | Meaning |
---|---|
anchor | When the text and/or image are smaller than the width, the anchor option determines where to position them tk.W , tk.CENTER or tk.E for left, center, and right alignment respectively. |
background | Set the background color for the label |
borderwidth | Add a border around the label. |
class_ | Specify a custom widget class name for changing the label’s appearance. |
compound | Specify how to display both text and image on the Label. |
cursor | Specify the mouse cursor’s appearance when the mouse is over the widget. |
font | Specify the font style for displaying text |
foreground | Specify the color of the text |
image | Specify an image or images to show in addition to text or instead of text. |
justify | If the text contains newline characters, the justify option specifies how each line is positioned horizontally. The valid values are tk.LEFT (left-justify), tk.CENTER (center), and tk.RIGHT (right-justify). |
padding | Add more space around the label. |
relief | Use this option to create an effect for the Label .e.g, flat, raised, sunken, groove, and ridge. |
style | Specify the custom widget style. |
takefocus | is a boolean value that specifies whether the label is visited during focus traversal. It defaults to False which doesn’t get focus. |
text | Specify a string of text to show in the widget |
textvariable | A StringVar instance that holds the text value of the widget. It overrides the text option if both textvariable and text are available. |
underline | Specify the position of the letter that should be underlined e.g, underline = 0 would underline the letter E in the text=’Exit’ |
width | Specify the number of characters to show |
wraplength | Chop the text into the lines which less than the length specified by the wraplength option. |
The following shows a skeleton program that we’ll use to illustrate various options of the Label widget:
import tkinter as tk from tkinter import ttk root = tk.Tk() root.geometry('300x200') root.resizable(False, False) root.title('Label Widget Demo') # show the label here root.mainloop()
Code language: Python (python)
Displaying a regular label
The following program shows how to display a regular label on the root window:
import tkinter as tk from tkinter.ttk import Label root = tk.Tk() root.geometry('300x200') root.resizable(False, False) root.title('Label Widget Demo') # show a label label = Label(root, text='This is a label') label.pack(ipadx=10, ipady=10) root.mainloop()
Code language: Python (python)
- First, import Label class from the tkinter.ttk module.
- Second, create the root window and set its properties including size, resizeable, and title.
- Third, create a new instance of the Label widget, set its container to the root window, and assign a literal string to its text property.
Setting a specific font for the Label
To set a particular font for a label, you pass the font keyword argument to the Label constructor like this:
font = ('font name', font_size)
Code language: Python (python)
The font keyword argument is a tuple that contains font name and size. For example:
font=("Helvetica", 14)
Code language: Python (python)
The following example shows a label with the Helvetica font:
import tkinter as tk from tkinter import ttk root = tk.Tk() root.geometry('300x200') root.resizable(False, False) root.title('Label Widget Demo') # label with a specific font label = ttk.Label( root, text='A Label with the Helvetica font', font=("Helvetica", 14)) label.pack(ipadx=10, ipady=10) root.mainloop()
Code language: Python (python)
Displaying an image
To use a Label widget to display an image, you follow these steps:
First, create a PhotoImage widget by passing the path to the photo to the PhotoImage constructor:
photo = tk.PhotoImage(file='./assets/python.png')
Code language: Python (python)
Second, assign the PhotoImage object to the image option of the Label widget:
Label(. image=photo)
Code language: Python (python)
The following example shows how to use a Label widget to display an image:
import tkinter as tk from tkinter import ttk # create the root window root = tk.Tk() root.geometry('300x200') root.resizable(False, False) root.title('Label Widget Image') # display an image label photo = tk.PhotoImage(file='./assets/python.png') image_label = ttk.Label( root, image=photo, padding=5 ) image_label.pack() root.mainloop()
Code language: Python (python)
Note that the image file is located at the /assets/ folder.
To display both text and image, you’ll use the text attribute and compound option.
The compound option specifies the position of the image relative to the text. Its valid values are:
Compound | Effect |
---|---|
‘top’ | Display the image above the text. |
‘bottom’ | Display the image below the text. |
‘left’ | Display the image to the left of the text. |
‘right’ | Display the image to the right of the text. |
‘none’ | Display the image if there’s one, otherwise display the text. The compound option defaults to ‘none’ . |
‘text’ | Display the text, not the image |
‘image’ | Display the image, not the text. |
The following program shows how to display both text and image on a label:
import tkinter as tk from tkinter import ttk # create the root window root = tk.Tk() root.geometry('300x200') root.resizable(False, False) root.title('Label Widget Image') # display an image label photo = tk.PhotoImage(file='./assets/python.png') image_label = ttk.Label( root, image=photo, text='Python', compound='top' ) image_label.pack() root.mainloop()
Code language: PHP (php)
Summary
Change the Tkinter Label Font Size
- Change the Tkinter Label Font Size
- Change the Tkinter Label Font Family
This tutorial guide demonstrates how to change the Tkinter label font size. We create two buttons Increase and Decrease to increase/decrease the Tkinter label font size.
Change the Tkinter Label Font Size
import tkinter as tk import tkinter.font as tkFont app = tk.Tk() fontStyle = tkFont.Font(family="Lucida Grande", size=20) labelExample = tk.Label(app, text="20", font=fontStyle) def increase_label_font(): fontsize = fontStyle['size'] labelExample['text'] = fontsize+2 fontStyle.configure(size=fontsize+2) def decrease_label_font(): fontsize = fontStyle['size'] labelExample['text'] = fontsize-2 fontStyle.configure(size=fontsize-2) buttonExample1 = tk.Button(app, text="Increase", width=30, command=increase_label_font) buttonExample2 = tk.Button(app, text="Decrease", width=30, command=decrease_label_font) buttonExample1.pack(side=tk.LEFT) buttonExample2.pack(side=tk.LEFT) labelExample.pack(side=tk.RIGHT) app.mainloop()
fontStyle = tkFont.Font(family="Lucida Grande", size=20)
We specify the font to be font family Lucida Grande with size of 20 , and assign it to be the font of label labelExample .
def increase_label_font(): fontsize = fontStyle['size'] labelExample['text'] = fontsize+2 fontStyle.configure(size=fontsize+2)
The font size is updated with tkinter.font.configure() method. The widget that uses this specific font will be updated automatically as you could see from the gif animation.
labelExample['text'] = fontsize+2
We also update the label text to be same with font size to make the animation more intuitive.
Change the Tkinter Label Font Family
We will also introduce how to change the Tkinter label font family by clicking the button.
import tkinter as tk import tkinter.font as tkFont app = tk.Tk() fontfamilylist = list(tkFont.families()) fontindex = 0 fontStyle = tkFont.Font(family=fontfamilylist[fontindex]) labelExample = tk.Label(app, text=fontfamilylist[fontindex], font=fontStyle) def increase_label_font(): global fontindex fontindex = fontindex + 1 labelExample.configure(font=fontfamilylist[fontindex], text=fontfamilylist[fontindex]) buttonExample1 = tk.Button(app, text="Change Font", width=30, command=increase_label_font) buttonExample1.pack(side=tk.LEFT) labelExample.pack(side=tk.RIGHT) app.mainloop()
fontfamilylist = list(tkFont.families())
It gets the available Tkinter font families list.
labelExample.configure(font=fontfamilylist[fontindex], text=fontfamilylist[fontindex])
The font property of labelExample will change to the next font in the font.families list, and the label text is also updated to the font name.
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
Related Article — Tkinter Label
Related Article — Tkinter Font
How to set the font size in Tkinter?
In this article, we are going to learn how to change the font size of the text in Tkinter. Font size refers to how large the characters displayed on the screen are. It is crucial to use proper font size in order to gain the reader’s attention wherever needed. So let’s see the different ways using which we can change the font size of text using Tkinter.
Method 1: Changing Tkinter font size using the font as a tuple
import tkinter as tk from tkinter import * #main window root = Tk() #title of the window root.title("Tkinter Font Size") #adding a label l = Label(root, text="This is a sample line with font size 15.", width=40, height=5, font=('Times New Roman', 15, 'bold')) l.pack() root.mainloop()
In the above code, we have created a very basic GUI and added a label to it. Label widgets have an inbuilt property of font(‘font family’, size, ‘font style’). Font acts as a parameter in the code above. If not mentioned explicitly, the parameters will have their default values. In the above code, we have only used the size property and set the size to 15.
Method 2: Changing tkinter font size using the font as an object
import tkinter as tk from tkinter import * import tkinter.font as tkFont #main window root = tk.Tk() #title of the window root.title("Tkinter Font Size") #creating a font object fontObj = tkFont.Font(size=28) #adding a label l = Label(root, text="This is a sample line with font size 28.", width=40, height=5, font=fontObj) l.pack() root.mainloop()
Here, we have created an object of the Font class named fontObj and set the font size to 28. Later, in the label, we assigned the value of parameters for the font to be equal to the font object (fontObj) and hence, in the output, the font size is 28. In addition to size, we can also mention the font family and style as required.
Method 3: Using changing fonts using a custom class
import tkinter as tk from tkinter import * from tkinter.font import Font #creating a custom class class cl: def __init__(self, master) -> None: self.master = master #setting the font size to be 40 self.customFont = Font(size=40) Label(self.master, text="Font size in the custom class is 40.", font=self.customFont).pack() #end of custom class if __name__ == "__main__": #main window root = tk.Tk() #title of the window root.title("Tkinter Font Size") #adding a label l = Label(root, text="This is a sample line with default font size.", width=40, height=5) l.pack() #calling our class object customSize = cl(root) root.mainloop()
In the above example, we have defined a custom class (cl), inside which we have a constructor where we assign the font size to 40. A label is also created with the font parameter being equal to the customFont.
In the main function, we first created a label with no explicit mention of the size of the text. Hence, it has the default size. After this, we call the cl class by creating its object customSize. On creating a new instance or object, the label with font size 40 is also displayed in the output along with the default size label.
Every time a new instance of the class is created, the label with font size 40 will be displayed as it is a part of the constructor of the class here. We can also set the font family and style inside the class itself.
Summary
In this way, we have understood how to change the font size in Tkinter in multiple ways. It is an easy topic to understand and implement. Please check out our other Tkinter tutorials here.