
Tkinter 可以使用几何布局管理器来组织窗口上的小部件。Tkinter 支持三种几何布局管理器:
packgridplace在本文中,将介绍 Tkinter 的几何布局管理器 pack 以及如何使用它在窗口上排列小部件。
下面,通过一个简单的示例来说明 pack 几何布局管理器的使用方法。
import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('几何布局管理器演示')button1 = tk.Button(root, text='1',bg='red', fg='white', width=20)button2 = tk.Button(root, text='2',bg='green', fg='white', width=20)button3 = tk.Button(root, text='3',bg='blue', fg='white', width=20)button1.pack()button2.pack()button3.pack()root.mainloop()
上面示例中,包含三个按钮,使用 pack() 进行布局 。默认情况下,小部件从上到下垂直居中排列。
pack() 支持多重参数进行自定义布局。
sideside 参数指定小部件布局向。该参数有四个可选选项:
'top':从上到下垂直排列小部件。'bottom':从下到上垂直排列小部件。'left':从左到右水平排列小部件。'right':从右到左水平排列小部件。还可以使用 Tkinter 模块提供的预定义常量:
tk.TOPtk.BOTTOMtk.LEFTtk.RIGHTimport tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('几何布局管理器演示')button1 = tk.Button(root, text='1',bg='red', fg='white', width=20)button2 = tk.Button(root, text='2',bg='green', fg='white', width=20)button3 = tk.Button(root, text='3',bg='blue', fg='white', width=20)button1.pack()button2.pack()button3.pack()button1 = tk.Button(root, text='1',bg='red', fg='white', width=20)button2 = tk.Button(root, text='2',bg='green', fg='white', width=20)button3 = tk.Button(root, text='3',bg='blue', fg='white', width=20)button1.pack(side=tk.BOTTOM)button2.pack(side=tk.BOTTOM)button3.pack(side=tk.BOTTOM)button1 = tk.Button(root, text='1',bg='red', fg='white', width=10)button2 = tk.Button(root, text='2',bg='green', fg='white', width=10)button3 = tk.Button(root, text='3',bg='blue', fg='white', width=10)button1.pack(side=tk.LEFT)button2.pack(side=tk.LEFT)button3.pack(side=tk.LEFT)button1 = tk.Button(root, text='1',bg='red', fg='white', width=10)button2 = tk.Button(root, text='2',bg='green', fg='white', width=10)button3 = tk.Button(root, text='3',bg='blue', fg='white', width=10)button1.pack(side=tk.RIGHT)button2.pack(side=tk.RIGHT)button3.pack(side=tk.RIGHT)root.mainloop()
expand 确定小部件是否应扩展以占用分配给容器的任何额外空间。
import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('几何布局管理器演示')button1 = tk.Button(root, text='1',bg='red', fg='white', width=20)button2 = tk.Button(root, text='2',bg='green', fg='white', width=20)button3 = tk.Button(root, text='3',bg='blue', fg='white', width=20)button1.pack(side=tk.TOP, expand=True)button2.pack(side=tk.TOP, expand=False)button3.pack(side=tk.TOP, expand=False)root.mainloop()
以上示例,button1 使用参数 expand=True,扩展占用了容器的剩余空间。
expand 参数依赖于 side 参数,小部件的最大宽度(高度)可以与容器一样宽(高)。
如果三个按钮都使用参数 expand=True,则平分容器空间。

fill 参数确定小组件是否会直接占用可用空间,改变小组件的宽度或高度。它接受以下值:
tk.Xtk.Ytk.BOTH默认情况下,填充为 none。
import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('几何布局管理器演示')button1 = tk.Button(root, text='1',bg='red', fg='white', width=20)button2 = tk.Button(root, text='2',bg='green', fg='white', width=20)button3 = tk.Button(root, text='3',bg='blue', fg='white', width=20)button1.pack(side=tk.TOP, expand=True, fill=tk.X)button2.pack(side=tk.TOP, expand=True, fill=tk.Y)button3.pack(side=tk.TOP, expand=True, fill=tk.BOTH)root.mainloop()
以上示例中,第一个按钮小部件沿 x 轴填充额外的空间。第二个按钮小部件沿 y 轴填充额外的空间。第三个按钮小部件在水平和垂直方向上占用任何额外的空间。
ipadx 、ipadyipadx 、ipady 参数为小部件创建内部填充,是组件文本跟组件边界之间的距离。ipadx 沿 x 轴创建填充。ipady 沿 Y 轴创建填充。
import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('几何布局管理器演示')button1 = tk.Button(root, text='1',bg='red', fg='white')button2 = tk.Button(root, text='2',bg='green', fg='white')button3 = tk.Button(root, text='3',bg='blue', fg='white')button4 = tk.Button(root, text='4',bg='yellow', fg='red')button1.pack(side=tk.LEFT)button2.pack(side=tk.LEFT, ipadx=40)button3.pack(side=tk.LEFT, ipady=40)button4.pack(side=tk.LEFT, ipadx=80, ipady=80)root.mainloop()
padx 、pady 参数分别指定外部水平和垂直填充,是组件跟邻近组件或窗体边界的距离。
import tkinter as tkroot = tk.Tk()root.geometry('600x400+200+200')root.title('几何布局管理器演示')button1 = tk.Button(root, text='1',bg='red', fg='white')button2 = tk.Button(root, text='2',bg='green', fg='white')button3 = tk.Button(root, text='3',bg='blue', fg='white')button4 = tk.Button(root, text='4',bg='yellow', fg='red')button1.pack(side=tk.LEFT, padx=20)button2.pack(side=tk.LEFT, padx=40)button3.pack(side=tk.LEFT, padx=60)button4.pack(side=tk.LEFT, padx=80)root.mainloop()
anchor 决定组件停靠的位置。


