Layout Managers in Tkinter
In Tkinter, a Layout Manager is used to arrange and position widgets (buttons, labels, text boxes, etc.) inside a window. Tkinter provides three main layout managers:
-
Pack()
-
Place()
-
Grid()
1. Pack Layout Manager
The pack() method organizes widgets vertically or horizontally in blocks. It automatically adjusts the position of widgets inside the window.
Features
-
Simple and easy to use
-
Widgets are arranged top, bottom, left, or right
Example Program
from tkinter import *
root = Tk()
Label(root, text="First Label").pack()
Label(root, text="Second Label").pack()
Label(root, text="Third Label").pack()
root.mainloop()
Output:
Three labels will appear one below another in the window.
2. Place Layout Manager
The place() method positions widgets using specific x and y coordinates. It gives exact control over the widget position.
Features
-
Uses absolute positioning
-
Allows setting x and y values
Example Program
from tkinter import *
root = Tk()
Label(root, text="Hello Tkinter").place(x=50, y=50)
root.geometry("200x200")
root.mainloop()
Output:
The label will appear at position (50,50) in the window.
3. Grid Layout Manager
The grid() method arranges widgets in a table format using rows and columns.
Features
-
Best for form designs
-
Uses row and column numbers
Example Program
from tkinter import *
root = Tk()
Label(root, text="Username").grid(row=0, column=0)
Entry(root).grid(row=0, column=1)
Label(root, text="Password").grid(row=1, column=0)
Entry(root).grid(row=1, column=1)
root.mainloop()
Output:
A simple login form layout with username and password fields arranged in rows and columns.
Comments
Post a Comment