1

当窗口重新调整大小时,这里尝试让 Widget 保持在屏幕的中心。我的意思是网格的正常行为,sticky='ew'其框架被打包以展开和fill='x'. 这是一些演示代码来说明我的意思:

from Tkinter import Frame,Button,Label
from ttk import Notebook

root = Frame()
root.pack(expand=True,fill='both')
nb = Notebook(root)

btn_f = Frame(nb)
Button(btn_f, text="Button Packed").pack(pady=100,padx=100)
# btn_f.pack(expand=True,fill='both') #makes no difference if this is removed

lbl_f = Frame(nb)
Label(lbl_f, text="This label is in a grid").grid(pady=100,sticky='ew')
# lbl_f.grid() #makes no difference if this is removed

nb.add(btn_f, text="Button")
nb.add(lbl_f, text="Label")

nb.pack(expand=True,fill='x')

root.mainloop()

我的怀疑与我发现的注释掉包和扩展有关。Notebook 中的 add 方法是否运行它自己的布局管理器来处理框架在其中的放置方式?我要问的是如何实现以网格为中心的效果,就像我在第一个选项卡中使用 pack 演示的那样?

4

1 回答 1

1

此代码使其行为与打包的button.

lbl_f = Frame(nb)
Label(lbl_f, text="This label is in a grid").grid(pady=100,sticky='ew')
lbl_f.grid()
lbl_f.rowconfigure('all', weight=1)
lbl_f.columnconfigure('all', weight=1)

如您所见,row/columnfigure应用于frame元素。


PS我建议你稍微修改你的代码。如果您像这样更改小部件(例如),它会使您在路上变得更容易:

Button(btn_f, text="Button Packed").pack(pady=100,padx=100) 

packedButton = Button(btn_f, text="Button Packed")
packedButton.pack(pady=100,padx=100) 

这样,您可以稍后参考按钮(或任何小部件)。但是,您不能在同一行上创建/打包(或网格)小部件;它必须单独关闭,如此处所示。

另一个积极的变化是使用类。SO上有很多示例,但是如果此问题中的代码只是一个快速示例,那么您将获得更大的力量。祝你好运!

于 2013-08-14T13:51:10.160 回答