Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
The Label
widget in tkinter
is used to display text or an image. It's one of the most commonly used widgets in tkinter
applications, especially when you want to present static information to the user.
tkinter
Tutorial:1. Import Required Libraries:
import tkinter as tk
2. Initialize Main Application Window:
root = tk.Tk() root.title("Label Widget Tutorial")
3. Creating a Basic Label:
label = tk.Label(root, text="Hello, Tkinter!") label.pack(padx=20, pady=20)
4. Some Commonly Used Options with Label:
text
: The text to be displayed.bg
: Background color.fg
: Foreground color (color of the text).font
: Defines the font type and size.image
: Used to display an image.anchor
: Alignment of the text or image inside the widget. Values can be N, NE, E, SE, S, SW, W, NW, or CENTER.relief
: Defines the type of the border. Some values are tk.FLAT
(default), tk.RAISED
, tk.SUNKEN
, tk.GROOVE
, and tk.RIDGE
.padx
and pady
: External padding around the text or image.Example:
label_custom = tk.Label(root, text="Custom Label", bg="yellow", fg="red", font=('Arial', 14, 'bold'), relief=tk.RAISED, padx=10, pady=10) label_custom.pack(pady=20)
5. Displaying an Image with Label:
To display an image in a label, use the Image
module from tkinter
.
img = tk.PhotoImage(file="path_to_image.png") # Use the appropriate path to your image img_label = tk.Label(root, image=img) img_label.pack(pady=20)
6. Combining Text and Image:
Using compound
option, you can have both text and image in a label. The compound
option specifies the position of the image relative to the text, e.g., tk.LEFT
, tk.TOP
, etc.
combo_label = tk.Label(root, text="Text and Image", image=img, compound=tk.LEFT) combo_label.pack(pady=20)
7. Mainloop to Run the Application:
root.mainloop()
Complete Code (excluding image-related code):
import tkinter as tk root = tk.Tk() root.title("Label Widget Tutorial") label = tk.Label(root, text="Hello, Tkinter!") label.pack(padx=20, pady=20) label_custom = tk.Label(root, text="Custom Label", bg="yellow", fg="red", font=('Arial', 14, 'bold'), relief=tk.RAISED, padx=10, pady=10) label_custom.pack(pady=20) root.mainloop()
When you run the code, you'll see a window with two labels, showcasing the default look and the customized look of the Label
widget. Adjust the properties as needed to get the desired appearance for your labels.
Python Tkinter create Label example:
Label
widget in Tkinter is used to display text or images.import tkinter as tk root = tk.Tk() # Create a Label label = tk.Label(root, text="Hello, Tkinter!") label.pack(padx=10, pady=10) root.mainloop()
How to use Label widget in Tkinter:
Label
widget is used to display text or images in a Tkinter GUI.import tkinter as tk root = tk.Tk() # Create a Label label = tk.Label(root, text="Hello, Tkinter!") label.pack(padx=10, pady=10) root.mainloop()
Displaying text and images with Label in Tkinter:
Label
widget can display both text and images. You can set the text
or image
option accordingly.import tkinter as tk root = tk.Tk() # Display text and image using Label label_text = tk.Label(root, text="Hello, Tkinter!") label_image = tk.Label(root, image=tk.PhotoImage(file="image.png")) label_text.pack(pady=10) label_image.pack(pady=10) root.mainloop()
Tkinter Label widget options and configuration:
Label
widget has various options for configuring its appearance, including options for font, background color, foreground color, etc.import tkinter as tk root = tk.Tk() # Customize the appearance of a Label label = tk.Label(root, text="Styled Label", font=("Arial", 12), bg="lightgray", fg="blue") label.pack(padx=10, pady=10) root.mainloop()
Handling events with Label widget in Tkinter:
Label
widget can respond to events, such as mouse clicks, by binding event handlers to it.import tkinter as tk def on_label_click(event): label.config(text="Clicked!") root = tk.Tk() # Create a Label with an event handler label = tk.Label(root, text="Click Me!") label.pack(padx=10, pady=10) label.bind("<Button-1>", on_label_click) root.mainloop()
Building layouts with Label in Tkinter:
Label
widget is commonly used to display information and contribute to the layout of Tkinter GUIs.import tkinter as tk root = tk.Tk() # Build a layout using Labels label1 = tk.Label(root, text="Label 1") label2 = tk.Label(root, text="Label 2") label3 = tk.Label(root, text="Label 3") label1.pack() label2.pack() label3.pack() root.mainloop()
Tkinter Label widget grid and pack methods:
Label
widget can be placed in the Tkinter window using either the pack
or grid
method for layout management.import tkinter as tk root = tk.Tk() # Use both pack and grid to manage Labels label1 = tk.Label(root, text="Label 1") label2 = tk.Label(root, text="Label 2") label1.pack(side=tk.LEFT) label2.grid(row=0, column=1) root.mainloop()