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()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示