How to explicitly resize frames in tkinter?

The Frame widget in tkinter serves as a container for organizing other widgets. You can explicitly control frame sizes using geometry managers like pack(), grid(), and place(), each offering different resizing capabilities.

Using pack() with fill and expand

The pack() geometry manager provides fill and expand options to control frame resizing ?

import tkinter as tk

# Create main window
root = tk.Tk()
root.geometry("600x400")
root.title("Frame Resizing with pack()")

# Create frames with different fill options
top_frame = tk.Frame(root, bg="lightblue", relief="raised", bd=2)
top_frame.pack(side="top", fill="x", padx=5, pady=5)

bottom_frame = tk.Frame(root, bg="lightgreen", relief="raised", bd=2)
bottom_frame.pack(side="bottom", fill="both", expand=True, padx=5, pady=5)

# Add widgets to frames
tk.Label(top_frame, text="Top Frame - fills horizontally", bg="lightblue").pack(pady=10)
tk.Label(bottom_frame, text="Bottom Frame - fills both directions", bg="lightgreen").pack(pady=20)

root.mainloop()

Using grid() for Precise Control

The grid() manager offers more precise control over frame sizing using sticky and weight configuration ?

import tkinter as tk

root = tk.Tk()
root.geometry("600x400")
root.title("Frame Resizing with grid()")

# Configure grid weights for resizing
root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(1, weight=2)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)

# Create frames with grid
frame1 = tk.Frame(root, bg="coral", relief="solid", bd=2)
frame1.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)

frame2 = tk.Frame(root, bg="gold", relief="solid", bd=2)
frame2.grid(row=0, column=1, sticky="nsew", padx=2, pady=2)

frame3 = tk.Frame(root, bg="lightcyan", relief="solid", bd=2)
frame3.grid(row=1, column=0, columnspan=2, sticky="nsew", padx=2, pady=2)

# Add labels
tk.Label(frame1, text="Frame 1", bg="coral").pack(expand=True)
tk.Label(frame2, text="Frame 2", bg="gold").pack(expand=True)
tk.Label(frame3, text="Frame 3 (spans 2 columns)", bg="lightcyan").pack(expand=True)

root.mainloop()

Fixed Size Frames with place()

Use place() for absolute positioning and fixed dimensions ?

import tkinter as tk

root = tk.Tk()
root.geometry("500x400")
root.title("Fixed Size Frames with place()")

# Create fixed-size frames
fixed_frame = tk.Frame(root, bg="mediumpurple", relief="raised", bd=3)
fixed_frame.place(x=50, y=50, width=200, height=100)

resizable_frame = tk.Frame(root, bg="lightpink", relief="raised", bd=3)
resizable_frame.place(x=300, y=50, width=150, height=100)

# Another frame with relative positioning
bottom_frame = tk.Frame(root, bg="lightsteelblue", relief="raised", bd=3)
bottom_frame.place(relx=0.1, rely=0.6, relwidth=0.8, relheight=0.3)

# Add content
tk.Label(fixed_frame, text="Fixed Size\n200x100", bg="mediumpurple").place(relx=0.5, rely=0.5, anchor="center")
tk.Label(resizable_frame, text="Also Fixed\n150x100", bg="lightpink").place(relx=0.5, rely=0.5, anchor="center")
tk.Label(bottom_frame, text="Relative Size (80% width, 30% height)", bg="lightsteelblue").place(relx=0.5, rely=0.5, anchor="center")

root.mainloop()

Comparison of Geometry Managers

Manager Best For Resizing Control Complexity
pack() Simple layouts fill, expand options Low
grid() Complex layouts Weight configuration Medium
place() Absolute positioning Fixed or relative dimensions High

Conclusion

Use pack() with fill and expand for simple resizing, grid() with weights for complex layouts, and place() for absolute control. Choose based on your layout requirements and desired resizing behavior.

Updated on: 2026-03-26T18:51:35+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements