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()