Python tkinter之pack
1、默认居中,从上而下
# -*- encoding=utf-8 -*- import tkinter from tkinter import * if __name__ == '__main__': pass win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 300 height = 100 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 Button(win, text='按钮1', bg='red').pack() # 默认居中,从上而下 Button(win, text='按钮2', bg='yellow').pack() Button(win, text='按钮3', bg='blue').pack() win.mainloop()
2、填充X,Y轴
2.1、填充X轴(fill=X)
# -*- encoding=utf-8 -*- import tkinter from tkinter import * if __name__ == '__main__': pass win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 300 height = 100 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 Button(win, text='按钮1', bg='red').pack( fill=X, # 填充横坐标 ) Button(win, text='按钮2', bg='yellow').pack() Button(win, text='按钮3', bg='blue').pack() win.mainloop()
2.2、填充Y轴(fill=Y)
# -*- encoding=utf-8 -*- import tkinter from tkinter import * if __name__ == '__main__': pass win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 300 height = 100 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 Button(win, text='按钮1', bg='red').pack( side=LEFT, # 放置在左边 fill=Y, # 填充纵坐标 ) Button(win, text='按钮2', bg='yellow').pack() Button(win, text='按钮3', bg='blue').pack() win.mainloop()
2.3、填充XY轴(fill=BOTH,expand=True)
# -*- encoding=utf-8 -*- import tkinter from tkinter import * if __name__ == '__main__': pass win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 300 height = 100 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 Button(win, text='按钮1', bg='red').pack( side=LEFT, # 放置在左边 fill=BOTH, # 填充 expand=True, # 如果父组件大小增长,则展开小部件 ) Button(win, text='按钮2', bg='yellow').pack() Button(win, text='按钮3', bg='blue').pack() win.mainloop()
3、内外间距
# -*- encoding=utf-8 -*- import tkinter from tkinter import * if __name__ == '__main__': pass win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 300 height = 300 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 Button(win, text='按钮1', bg='red', width=20).pack( ipadx=10, # 内间距x ipady=10, # 内间距y padx=10, # 外间距x pady=10 # 外间距y ) Button(win, text='按钮2', bg='yellow', width=20).pack() Button(win, text='按钮3', bg='blue').pack() win.mainloop()
备注:
①按钮1和按钮2的宽度一致,但是按钮1的ipadx=10,可以看出按钮1比按钮二长10
②按钮1设置了X和Y的外间距,因此可以看出组件间的间隔
4、side布局
4.1、上下左右布局
# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
if __name__ == '__main__':
pass
win = tkinter.Tk() # 窗口
win.title('南风丶轻语') # 标题
screenwidth = win.winfo_screenwidth() # 屏幕宽度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 300
height = 100
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置
Button(win, text='按钮1', bg='red').pack(side=RIGHT)
Button(win, text='按钮2', bg='yellow').pack(side=LEFT)
Button(win, text='按钮3', bg='blue').pack(side=BOTTOM)
Button(win, text='按钮4', bg='green').pack(side=TOP)
win.mainloop()
4.2、从左(右)向右(左)
# -*- encoding=utf-8 -*- import tkinter from tkinter import * if __name__ == '__main__': pass win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 300 height = 100 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 Button(win, text='按钮1', bg='red').pack(side=LEFT) Button(win, text='按钮2', bg='yellow').pack(side=LEFT) Button(win, text='按钮3', bg='blue').pack(side=LEFT) Button(win, text='按钮4', bg='green').pack(side=LEFT) Button(win, text='按钮5', bg='red').pack(side=RIGHT) Button(win, text='按钮6', bg='yellow').pack(side=RIGHT) Button(win, text='按钮7', bg='blue').pack(side=RIGHT) Button(win, text='按钮8', bg='green').pack(side=RIGHT) win.mainloop()
4.3、自动填充满父元素
# -*- encoding=utf-8 -*- import tkinter from tkinter import * if __name__ == '__main__': pass win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 400 height = 500 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 all_count = 20 line_count = 4 color = ['red', 'blue', 'yellow', 'pink', 'green'] color_index = 0 for j in range(int(all_count / line_count)): f = Frame() f.pack(fill=BOTH, expand=True) for i in range(line_count): Button(f, text='排列按钮', bg=color[color_index]).pack(side='left', expand='true', fill=BOTH) color_index += 1 if color_index >= len(color): color_index = 0 win.mainloop()