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 tkinter GUI layout using frames and grid
Tkinter is a well-known Python library for creating GUI-based applications. You can create fully featured applications using widgets, functions, and modules that are already present in the Tkinter library.
The Frame widget in Tkinter manages all widgets in an application as a container. It inherits all properties that the main window can contain. To design the layout of widgets, we can use geometry managers like Grid, which places widgets using a coordinate system with row and column properties.
Basic Frame and Grid Structure
Here's a simple example showing how to create a frame and use grid layout ?
from tkinter import *
# Create main window
root = Tk()
root.title("Grid Layout Example")
root.geometry("300x200")
# Create a frame
frame = Frame(root, bg="lightgray", padx=10, pady=10)
frame.pack(fill="both", expand=True)
# Add widgets using grid
Label(frame, text="Name:").grid(row=0, column=0, sticky="w", pady=5)
Entry(frame).grid(row=0, column=1, pady=5, padx=(10,0))
Label(frame, text="Email:").grid(row=1, column=0, sticky="w", pady=5)
Entry(frame).grid(row=1, column=1, pady=5, padx=(10,0))
Button(frame, text="Submit").grid(row=2, column=1, pady=10)
root.mainloop()
Registration Form Example
Let's create a more complete registration form using frames and grid layout ?
from tkinter import *
# Create main window
win = Tk()
win.title("Registration Form")
win.geometry("400x300")
win.configure(bg="white")
# Create main frame
frame = Frame(win, bg="lightblue", padx=20, pady=20)
frame.pack(fill="both", expand=True, padx=20, pady=20)
# Title
Label(frame, text="Registration Form",
font=('Arial', 16, 'bold'),
bg="lightblue").grid(row=0, column=0, columnspan=2, pady=(0,20))
# Form fields with proper grid alignment
Label(frame, text="First Name:", bg="lightblue").grid(row=1, column=0, sticky="w", pady=5)
fname = Entry(frame, width=25)
fname.grid(row=1, column=1, pady=5, padx=(10,0))
Label(frame, text="Last Name:", bg="lightblue").grid(row=2, column=0, sticky="w", pady=5)
lname = Entry(frame, width=25)
lname.grid(row=2, column=1, pady=5, padx=(10,0))
Label(frame, text="Email:", bg="lightblue").grid(row=3, column=0, sticky="w", pady=5)
email = Entry(frame, width=25)
email.grid(row=3, column=1, pady=5, padx=(10,0))
Label(frame, text="Password:", bg="lightblue").grid(row=4, column=0, sticky="w", pady=5)
password = Entry(frame, show="*", width=25)
password.grid(row=4, column=1, pady=5, padx=(10,0))
# Function to handle registration
def register():
if fname.get() and email.get():
result_label.config(text=f"Welcome {fname.get()}!", fg="green")
else:
result_label.config(text="Please fill required fields", fg="red")
# Submit button
Button(frame, text="Register", command=register,
bg="darkblue", fg="white").grid(row=5, column=1, pady=20, sticky="e")
# Result label
result_label = Label(frame, text="", bg="lightblue")
result_label.grid(row=6, column=0, columnspan=2, pady=5)
win.mainloop()
Key Grid Properties
| Property | Description | Example |
|---|---|---|
row, column |
Position in grid | grid(row=0, column=1) |
sticky |
Alignment (N,S,E,W) |
sticky="w" (left align) |
padx, pady |
External spacing | padx=10, pady=5 |
columnspan |
Span multiple columns | columnspan=2 |
Advanced Layout with Multiple Frames
You can create complex layouts by using multiple frames ?
from tkinter import *
root = Tk()
root.title("Multi-Frame Layout")
root.geometry("500x400")
# Top frame for header
header_frame = Frame(root, bg="navy", height=60)
header_frame.pack(fill="x")
header_frame.pack_propagate(False)
Label(header_frame, text="Dashboard",
font=('Arial', 20, 'bold'),
fg="white", bg="navy").pack(pady=15)
# Main content area
content_frame = Frame(root, bg="lightgray")
content_frame.pack(fill="both", expand=True, padx=10, pady=10)
# Left sidebar
sidebar = Frame(content_frame, bg="darkgray", width=150)
sidebar.pack(side="left", fill="y", padx=(0,10))
sidebar.pack_propagate(False)
Label(sidebar, text="Menu", font=('Arial', 12, 'bold'),
bg="darkgray", fg="white").pack(pady=10)
for item in ["Home", "Profile", "Settings"]:
Button(sidebar, text=item, width=15).pack(pady=5)
# Right content area with grid
main_area = Frame(content_frame, bg="white")
main_area.pack(side="right", fill="both", expand=True)
# Use grid in main area
Label(main_area, text="Content Area",
font=('Arial', 14, 'bold')).grid(row=0, column=0, pady=20)
Label(main_area, text="Status:").grid(row=1, column=0, sticky="w", padx=20)
Label(main_area, text="Active", fg="green").grid(row=1, column=1, sticky="w")
root.mainloop()
Conclusion
Frames provide excellent organization for tkinter GUI layouts, while the grid geometry manager offers precise control over widget positioning. Use sticky for alignment, columnspan for spanning cells, and combine multiple frames for complex layouts.
