tkinter pack 方法的“填充”和“扩展”选项之间的区别

Tkinter Pack Manager 的行为基于使用父部件内的额外空间。在打包小部件时,我们可以指定小部件是缩小还是填满整个屏幕。为了使小部件能够在整个窗口中增长,我们可以使用“填充”属性。它有助于通过添加“x”作为水平,“y”作为垂直或“BOTH”来填充屏幕中的小部件。

每当我们使用expand(boolean)属性时,我们通常都会调整小部件的大小以扩展其可用空间。它采用布尔值作为 true 或 false。当小部件的 expand 属性为真时,这意味着我们可以启用 grow 属性。另一方面,如果将 expand 属性设置为 false,则意味着我们将禁用小部件的 grow 属性。

示范

#Import tkinter library
from tkinter import *

#Create an instance of tkinter frame
win= Tk()

#Set the Geometry
win.geometry("750x250")

#Create a Labelwin= Tk()
Label(win, text= "Hey! There",font=('Helvetica 25 bold'), background= "burlywood1").pack(fill=BOTH, expand=1,padx=20,pady=20)
win.mainloop()
输出结果

执行上面的代码片段将显示一个全宽文本窗口。

现在,将屏幕稍微扩大一点,可以看到文本在整个窗口中调整大小的变化。