赞
踩
目录
一个GUI应用程序必然有大量的组件,这些组件如何排布?这时候就需要使用tkinter提供的布局管理器帮助我们组织,管理再父组件中子组件的布局方式,tkinter提供了三种管理器:pack、grid、place
grid表格布局,采用表格结构组织组件,子组件的位置由行和列的单元格确定,并且可以跨行和跨列,从而实现复杂的布局。
选项 | 说明 | 取值范围 |
---|---|---|
column | 单元格的列号 | 从0开始的正整数 |
columnspan | 跨列、跨越的列数 | 正整数 |
row | 单元格的行号 | 从0开始的正整数 |
rowspan | 跨行、跨越的列数 | 正整数 |
ipadx、ipady | 设置子组件之间的间隔,x方向或者y方向,默认单位为像素 | 非浮点数,默认0.0 |
padx、pady | 与之并列的组件之间的间隔,x方向或者y方向,默认单位为像素 | 非浮点数,默认0.0 |
sticky | 组件紧贴所在单元格的某一角,对应于东南西北种以及四个角 | "N"、"S"、"W"、"E"、"NW"、 "SW"、"SE"、"NE"、"center"(默认) |
代码演示
- from tkinter import *
- import tkinter as tk
-
-
- class Application(tk.Frame):
- def __init__(self, master=None):
- tk.Frame.__init__(self,master)
- self.master = master
- self.pack()
-
- self.createWidget()
-
- def createWidget(self):
- '''通过grid布局实现登录界面'''
- self.label01 = Label(self, text='用户名')
- self.label01.grid(row=0, column=0)
- self.entry01 = Entry(self)
- self.entry01.grid(row=0, column=1)
- Label(self, text='用户名为手机号').grid(row=0, column=2)
-
- Label(self, text='密码').grid(row=1, column=0)
- Entry(self, show='*').grid(row=1, column=1)
-
- Button(self, text='登录').grid(row=2, column=1, stick=EW)
- Button(self, text='取消').grid(row=2, column=2, stick=E)
-
- if __name__ == '__main__':
- root = Tk()
- root.geometry('300x100+200+300')
- root.title('登录系统')
- app = Application(root)
- root.mainloop()

- from tkinter import *
- import tkinter as tk
-
-
- class Application(tk.Frame):
- def __init__(self, master=None):
- tk.Frame.__init__(self, master)
- self.master = master
- self.pack()
-
- self.createWidget()
-
- def createWidget(self):
- '''通过grid布局实现计算器的界面'''
- btnText = (('MC', 'M+','M-','MR'),
- ('C', '±', '÷', '×'),
- (7, 8, 9, '-'),
- (4, 5, 6, '+'),
- (1, 2, 3, '='),
- (0, '.'),
- )
-
- Entry(self).grid(row=0, column=0, columnspan=4, pady=10)
-
- for rindex,r in enumerate(btnText):
- for cindex,c in enumerate(r):
- if c == '=':
- Button(self, text=c, width=2).grid(row=rindex + 1, column=cindex, rowspan=2, sticky=NSEW)
- elif c == 0:
- Button(self, text=c, width=2).grid(row=rindex + 1, column=cindex, columnspan=2, sticky=NSEW)
- elif c == '.':
- Button(self, text=c, width=2).grid(row=rindex + 1, column=cindex+1, rowspan=2, sticky=NSEW)
- else:
- Button(self, text=c, width=2).grid(row=rindex+1, column=cindex, sticky=NSEW)
-
- if __name__ == '__main__':
- root = Tk()
- root.title('计算器界面')
- root.geometry('200x230+200+300')
- app = Application(root)
- root.mainloop()

pack按照组件的创建顺序将子组件添加到父组件中,按照垂直或者水平的方向自然排布,如果不指定任何选项,默认在父组件中自顶向下垂直添加组件。
pack是代码量最少,最简单的一种,可以用于快速生成界面。
名称 | 描述 | 数值范围 |
---|---|---|
fill | 填充x(y)方向上的空间,当属性side=“top”或“bottom”时,填充x方向;当属性side=“left”或“right”时填充y方向;当expend选项为“yes”时,填充父组件的剩余空间 | “x”、“y”、“both”、“none”(默认值为none) |
ipadx、ipady | 设置子组件之间的间隔,x方向或者y方向,默认单位为像素 | 非浮点数,默认0.0 |
padx、pady | 与之并列的组件之间的间隔,x方向或者y方向,默认单位为像素 | 非浮点数,默认0.0 |
side | 定义停留在父组件的哪一边 | “top”、“bottom”、“left”、“right”(默认值为“top”) |
anchor | 对齐方式,左对齐“w”,右对齐“e”、顶对齐“n”、低对齐“s” | “N”、“S”、“W”、“E”、“NW”、“SW”、“SE”、“NE”、“center”(默认) |
- from tkinter import *
-
- root = Tk()
- root.geometry('700x220')
- root.title('钢琴按键')
-
- # Frame是一个矩形区域,就是用来放置其他子组件
- f1 = Frame(root)
- f1.pack()
- f2 = Frame(root)
- f2.pack()
-
- btnText = ('流行风','中国风','日本风','重金属','轻音乐')
-
- for txt in btnText:
- Button(f1, text=txt).pack(side='left', padx=10)
-
- for i in range(1,13):
- Button(f2, width=5, height=10, borderwidth=1, relief='solid',
- bg='black' if i%2==0 else 'white').pack(side='left', padx=2)
-
- root.mainloop()

place 布局管理器可以通过坐标精确控制组件的位置,适用于一些布局更加灵活的场景。
选项 | 说明 | 取值范围 |
---|---|---|
x、y | 组件左上角的绝对坐标(相对于窗口) | 非负整数 x和y选项用于设置偏移(像素),如果同时设置 relx(rely) 和 x(y) 那么place将优先计算relx和rely,然后再实现x和y指定的偏移值 |
relx、rely | 组件左上角的坐标(相对于父容器) | relx是相对于父组件的位置,0是最左边,0.5是正中间,1是最右边; rely是相对于父组件的位置,0是最左边,0.5是正中间,1是最右边; |
width、height | 组件的宽度和高度 | 非负数 |
rewidth、reheight | 组建的宽度和高度 | 与relx、rely取值类似,但是相对于父组件的尺寸 |
anchor | 对齐方式,左对齐“w”,右对齐“e”、顶对齐“n”、低对齐“s” | “N”、“S”、“W”、“E”、“NW”、“SW”、“SE”、“NE”、“center”(默认) |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。