Application layout in Tkinter is controlled with geometry managers (pack, place, grid). Different frames can use different geometry managers.
The geometry manager pack uses a packing algorithm to place widgets in a Frame or window in a specified order.
The fill keyword argument can be used to specify in which direction the frames should fill.
The side keyword argument can be used to specify on which side of the window the widget should be placed.
The anchor keyword argument can be used to specify the position of a widget within its allocated space.
The method widget.pack_forget() erases a drawn widget.
fill=tk.X # horizontal direction fill=tk.Y # vertical direction fill=tk.BOTH # or fill="both" side=tk.TOP # default, side="top" side=tk.BOTTOM # or side="bottom" side=tk.LEFT # or side="left" side=tk.RIGHT # or side="right" expand=tk.YES # or expand=True anchor=tk.N # or tk.S, tk.E, tk.W, tk.CENTER (default) anchor=tk.NE # or tk.NW, tk.SE, tk.SW
# pack1.py import tkinter as tk # Py3 root = tk.Tk() frame1 = tk.Frame(root, width=200, height=200, bg="red") frame1.pack() frame2 = tk.Frame(root, width=100, height=100, bg="yellow") frame2.pack() frame3 = tk.Frame(root, width=50, height=50, bg="blue") frame3.pack() root.mainloop() # run the tkinter event loop
# pack2.py import tkinter as tk # Py3 root = tk.Tk() # empty root frame1 = tk.Frame(root, width=200, height=200, bg="red") frame1.pack(fill=tk.X) frame2 = tk.Frame(root, height=100, bg="yellow") # no width frame2.pack(fill=tk.X) frame3 = tk.Frame(root, height=50, bg="blue") # no width frame3.pack(fill=tk.X) root.mainloop() # run the tkinter event loop
# pack3.py import tkinter as tk # Py3 root = tk.Tk() frame1 = tk.Frame(root, width=200, height=200, bg="red") frame1.pack(fill=tk.Y, side=tk.LEFT) frame2 = tk.Frame(root, width=100, bg="yellow") # no height frame2.pack(fill=tk.Y, side=tk.LEFT) frame3 = tk.Frame(root, width=50, bg="blue") # no height frame3.pack(fill=tk.Y, side=tk.LEFT) root.mainloop() # run the tkinter event loop
# pack4.py ('fill' and 'expand' keyword arguments)
import tkinter as tk # Py3
root = tk.Tk()
frame1 = tk.Frame(root, width=200, height=200, bg="red")
frame1.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
frame2 = tk.Frame(root, width=100, bg="yellow") # no height
frame2.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
frame3 = tk.Frame(root, width=50, bg="blue") # no height
frame3.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
root.mainloop() # run the tkinter event loop