在笔记本内扩展不能按预期工作

时间:2013-08-09 10:16:25

标签: python python-2.7 tkinter

以下是尝试在窗口重新调整大小时让窗口小部件保持在屏幕中央。我的意思是网格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的第一个选项卡中演示的那样?

1 个答案:

答案 0 :(得分: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元素。


P.S。 我建议你稍微修改你的代码。如果您更改小部件(例如),它将使您的工作更轻松:

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上有很多例子,但是如果这个问题中的代码只是一个快速的样本,那么你就会有更多的权力。祝你好运!

相关问题