Python - GUI Programming - Label Widget
The Label widget in Tkinter is used to display text or images on the window. It is one of the most commonly used widgets in GUI programming. The label widget can display static text or images. We can configure the label with the help of the text parameter to set the text or image parameter to set an image on the widget.
Here is an example of how to create a label widget in Tkinter:
import tkinter as tk
root = tk.Tk()
root.title("Label Widget Example")
label = tk.Label(root, text="Hello, World!")
label.pack()
root.mainloop()
In this example, we have created a label widget and set its text to "Hello, World!". We then used the pack() method to display the label widget on the window.
We can customize the look of the label widget by using various parameters such as font size, font type, font weight, foreground color, background color, etc. Here is an example of how to customize the label widget:
import tkinter as tk
root = tk.Tk()
root.title("Customized Label Widget Example")
label = tk.Label(root, text="Hello, World!", font=("Arial", 18), fg="white", bg="blue")
label.pack()
root.mainloop()
In this example, we have customized the label widget by changing the font type to Arial, font size to 18, foreground color to white, and background color to blue.
We can also display an image on the label widget using the PhotoImage class from the tkinter module. Here is an example of how to display an image on the label widget:
import tkinter as tk
root = tk.Tk()
root.title("Label Widget with Image Example")
photo = tk.PhotoImage(file="image.png")
label = tk.Label(root, image=photo)
label.pack()
root.mainloop()
In this example, we have used the PhotoImage class to load an image from a file called "image.png" and then displayed it on the label widget using the image parameter.
In conclusion, the label widget is a simple but powerful widget in Tkinter that allows us to display text and images on the window. We can customize the look of the label widget to fit our needs, and it is one of the most commonly used widgets in GUI programming.