Python Tkinter pack() 方法 ,此几何管理器在将 Widget 放置在父 Widget 中之前将其组织成块。
语法
widget.pack( pack_options )
以下是可能的选项列表 -
- expand − 当设置为 true 时,widget 会扩展以填充 widget 的父级中未使用的任何空间。
- fill − 确定 widget 是填充打包程序分配给它的任何额外空间,还是保留自己的最小尺寸:NONE(默认)、X(仅水平填充)、Y (仅垂直填充)或 BOTH(水平和垂直填充)。
- side − 确定父 widget 的哪一侧打包:TOP(默认)、BOTTOM、LEFT 或 RIGHT。
通过在不同的按钮上移动光标来尝试以下示例 -
from tkinter import *
root = Tk()
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
redbutton = Button(frame, text="Red", fg="red")
redbutton.pack( side = LEFT)
greenbutton = Button(frame, text="Brown", fg="brown")
greenbutton.pack( side = LEFT )
bluebutton = Button(frame, text="Blue", fg="blue")
bluebutton.pack( side = LEFT )
blackbutton = Button(bottomframe, text="Black", fg="black")
blackbutton.pack( side = BOTTOM)
root.mainloop()
执行上述代码时,它会产生以下结果 -
