In this lesson we want to learn about Python Tkinter Label, A Label in Tkinter is a widget that is used to display text or an image on a GUI (Graphical User Interface). It is a non-editable widget that can be used to display any type of text, such as a title, instructions, or a message.
The Tkinter Label widget can be created by calling the Label()
function, which takes one or more options as arguments. Some of the common options that can be passed to the Label widget are:
text
: The text to be displayed on the label.font
: The font to be used for the text.fg
: The color of the text.bg
: The background color of the label.image
: An image to be displayed on the label.
Here is an example of creating a simple label in Tkinter:
1 2 3 4 5 6 |
import tkinter as tk root = tk.Tk() label = tk.Label(root, text="Hello World", font=("Arial", 20)) label.pack() root.mainloop() |
In the above example, we first import the tkinter module, create a Tk object (root) and then create a label widget by calling the Label() function and passing the root window and text options. The pack()
method is used to place the label on the screen and the mainloop()
method is called to start the event loop and display the label on the screen.
Run the complete code and this will be the result
Learn More on TKinter
- How to Create Conutdown Timer with Python & TKinter
- Create GUI Applications with Python & TKinter
- Python TKinter Layout Management
- How to Create Label in TKinter
You can also use grid
or place
layout management in tkinter
1 2 3 4 5 6 7 8 9 10 11 12 |
import tkinter as tk root = tk.Tk() label = tk.Label(root, text="Hello World", font=("Arial", 20)) label.grid(row=0, column=0) root.mainloop() # or root = tk.Tk() label = tk.Label(root, text="Hello World", font=("Arial", 20)) label.place(x=20, y=20) root.mainloop() |
In the above example, grid layout is used to place the label on the screen and place layout is used to place the label on the specific position.