Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Creating a LabelFrame inside a Tkinter Canvas
Tkinter provides many built-in widgets for creating desktop applications. The LabelFrame widget creates a container with a labeled border, while Canvas provides a drawing surface. You can combine these by placing a LabelFrame inside a Canvas widget.
A LabelFrame widget has two main components:
The Title Bar − displays the label text at the top border
The Content Area − contains child widgets like labels, buttons, or images
Basic LabelFrame in Canvas
Here's how to create a LabelFrame inside a Canvas widget ?
from tkinter import *
# Create main window
win = Tk()
win.geometry("700x350")
win.title("LabelFrame in Canvas")
# Create a canvas widget
canvas = Canvas(win, bg="lightgray")
canvas.pack(fill=BOTH, expand=True)
# Create a LabelFrame widget inside canvas
lf = LabelFrame(canvas, text="Welcome Window", font=("Arial", 12, "bold"))
# Add a label inside the LabelFrame
label = Label(lf, text="This text is inside the LabelFrame.")
label.config(font="Arial 12")
label.pack(padx=20, pady=20)
# Pack the LabelFrame
lf.pack(pady=20)
win.mainloop()
Positioning LabelFrame with Canvas Window
For more precise positioning, use create_window() method ?
from tkinter import *
# Create main window
win = Tk()
win.geometry("700x350")
win.title("Positioned LabelFrame")
# Create canvas
canvas = Canvas(win, bg="white")
canvas.pack(fill=BOTH, expand=True)
# Create LabelFrame
lf = LabelFrame(win, text="User Information", font=("Arial", 10, "bold"))
# Add widgets inside LabelFrame
Label(lf, text="Name:", font=("Arial", 9)).grid(row=0, column=0, sticky=W, padx=5, pady=5)
Entry(lf, width=20).grid(row=0, column=1, padx=5, pady=5)
Label(lf, text="Email:", font=("Arial", 9)).grid(row=1, column=0, sticky=W, padx=5, pady=5)
Entry(lf, width=20).grid(row=1, column=1, padx=5, pady=5)
Button(lf, text="Submit").grid(row=2, column=0, columnspan=2, pady=10)
# Position LabelFrame in canvas at coordinates (100, 50)
canvas.create_window(100, 50, window=lf, anchor=NW)
win.mainloop()
Multiple LabelFrames in Canvas
You can add multiple LabelFrames at different positions ?
from tkinter import *
win = Tk()
win.geometry("800x500")
win.title("Multiple LabelFrames")
canvas = Canvas(win, bg="lightblue")
canvas.pack(fill=BOTH, expand=True)
# First LabelFrame
lf1 = LabelFrame(win, text="Personal Info", font=("Arial", 10, "bold"))
Label(lf1, text="Name: John Doe", font=("Arial", 9)).pack(padx=10, pady=5)
Label(lf1, text="Age: 25", font=("Arial", 9)).pack(padx=10, pady=5)
# Second LabelFrame
lf2 = LabelFrame(win, text="Contact Info", font=("Arial", 10, "bold"))
Label(lf2, text="Phone: 123-456-7890", font=("Arial", 9)).pack(padx=10, pady=5)
Label(lf2, text="Email: john@example.com", font=("Arial", 9)).pack(padx=10, pady=5)
# Position both LabelFrames in canvas
canvas.create_window(50, 50, window=lf1, anchor=NW)
canvas.create_window(300, 50, window=lf2, anchor=NW)
win.mainloop()
Key Methods and Options
| Method/Option | Description | Example |
|---|---|---|
text |
Label text for the frame | text="User Data" |
font |
Font for the label text | font=("Arial", 12) |
create_window() |
Position widget in canvas | canvas.create_window(x, y, window=widget) |
anchor |
Positioning anchor point |
anchor=NW (top-left) |
Conclusion
LabelFrame widgets inside Canvas provide flexible positioning and organization of related controls. Use pack() for simple layouts or create_window() for precise positioning within the canvas area.
