python - 在笔记本内扩展无法按预期工作

标签 python python-2.7 tkinter

这里尝试让 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 演示的那样?

最佳答案

此代码使其行为与打包的按钮相同。

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 元素。

<小时/>

附注 我建议你稍微修改一下你的代码。如果您这样更改小部件(例如),那么以后的事情会变得更容易:

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 有很多示例,但是如果这个问题中的代码只是一个快速示例,那么您将拥有更多能力。祝你好运!

关于python - 在笔记本内扩展无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18144457/

相关文章:

python - Pandas 相当于 data.table

python - 如何在单元测试中停止来自 subprocess.check_call() 的 AssertionError 消息

OSX 上的 Django 安装

Python错误: cannot import name cross_validation

python - Tkinter : Make canvas draggable

Python - Tkinter - 在列表中设置 IntVar

python - 如何从 QtGui.QListWidget 获取当前 Item 的信息?

python - 如何转义 Python 字符串中的任何特殊 shell 字符?

python-2.7 - Conda env 没有加载正确版本的 numpy

python - tkinter 左键单击 GUI 中的 TAB。如何检测用户何时执行此操作