Python 3 – Tkinter Label
Introduction
Tkinter is a built-in module in Python that enables the creation of Graphical User Interfaces (GUIs). It is a highly accomplished toolkit that provides various widgets such as labels, buttons, radio buttons, check buttons, and more. In this article, we will explore the Label widget in Tkinter.
What is a Label widget?
The Label widget is one of the most commonly used widgets in Tkinter. As the name suggests, it is used to display text or an image on the screen. We can specify the text to be displayed, the font style, font size, and font color.
Here is a sample code to create a Label widget:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="This is a Label")
label.pack()
root.mainloop()
In the above code, we first import the tkinter
module and create an instance of the Tk()
class, which will be used to create the main window of the application. Next, we create a Label widget using the Label()
method of the tkinter
module. We pass the root window and the text to be displayed in the widget as arguments. Finally, we use the pack()
method to display the widget on the screen.
Let us examine some of the Label widget’s essential parameters.
Parameters
Parameter 1: Text
The text parameter sets the text that should be displayed inside the label, as shown below:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="This is a Label", font=("Arial Bold", 14))
label.pack()
root.mainloop()
Here we set the text parameter to “This is a Label”. We can also set the font style and font size by using the font parameter. In this example, we use the Arial Bold font with a size of 14.
Parameter 2: Anchor
The anchor parameter corresponds to the widget’s anchor point. It determines where the text should be placed over the label. Here is a sample code:
import tkinter as tk
root = tk.Tk()
label1 = tk.Label(root, text="This is a Label1", font=("Arial Bold", 14), anchor='w')
label2 = tk.Label(root, text="This is a Label2", font=("Arial Bold", 14), anchor='e')
label3 = tk.Label(root, text="This is a Label3", font=("Arial Bold", 14), anchor='center')
label1.pack()
label2.pack()
label3.pack()
root.mainloop()
In the above example, we create three Label widgets with the text parameter set to different values. Moreover, we define the anchor parameter to “w”, “e”, and “center”, respectively. These values determine whether the text should be placed at the west (left), east (right), or the center of the label.
Parameter 3: Font
We have already seen how the font parameter works. Here is a sample code that demonstrates how to use font parameters:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="This is a Label", font=("Arial Bold", 14))
label.pack()
root.mainloop()
In the example, we set the font to Arial Bold with a size of 14.
Parameter 4: Background and Foreground
The background parameter sets the widget’s background color, while the foreground parameter sets the text color. Here is a sample code:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="This is a Label", font=("Arial Bold", 14), bg="blue", fg="white")
label.pack()
root.mainloop()
In the example code, we use the bg
parameter to set the label’s background color to blue and the fg
parameter to set the text color to white.
Parameter 5: Width and Height
The width and height parameters determine the dimensions of the widget. Here is a sample code:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="This is a Label", font=("Arial Bold", 14), width=20, height=5)
label.pack()
root.mainloop()
In the example, we set the width parameter to 20 and the height parameter to 5. The dimensions of the widget are now 20 characters wide and 5 characters high.
Conclusion
In this article, we explored the Label widget in Tkinter. We discussed the various parameters of the Label widget that can be used to modify the widget’s appearance. We also demonstrated how to use these parameters in sample code. We hope that you now have an excellent understanding of the Label widget and its use in Python GUI programming.