Python Tkinter 网格几何粘性设置无效(?) [英] Python Tkinter grid-geometry sticky setting not effective(?)

查看:85
本文介绍了Python Tkinter 网格几何粘性设置无效(?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 grid 几何体在 Python/Tkinter 中设计简单的输入对话框,并获得了一些意想不到的行为.当我开始使用此代码时:

I'm designing simple entry dialog in Python/Tkinter using the grid geometry, and getting some unexpected behavior. When I start out with this code:

winAddNew = tk.Toplevel()
winAddNew.title('Add New Customer')
lblName = tk.Label(winAddNew,
                   anchor=tk.W,font=fntNormal,
                   text='Enter the complete name of the customer:')
entName = tk.Entry(winAddNew,
                   justify=tk.LEFT, font=fntNormal, width=20,
                   textvariable=ctrlNewCustomerName)
lblID = tk.Label(winAddNew,
                 anchor=tk.W,font=fntNormal,
                 text='Enter a unique three-character ID:')
entID = tk.Entry(winAddNew,
                 justify=tk.LEFT, font=fntNormal, width=8,
                 textvariable=ctrlNewCustomerID)
lblName.grid(row=0,column=0,columnspan=2,sticky=tk.W,padx=10,pady=(10,0))
entName.grid(row=1,column=0,columnspan=2,sticky=tk.W,padx=10,pady=(5,0))
lblID.grid(row=2,column=0,sticky=tk.W,padx=(10,0),pady=10)
entID.grid(row=2,column=1,sticky=tk.W,padx=(0,10),pady=10)

...这是渲染窗口在我的机器上的外观(Windows 7,Python 2.7.3).请注意,绿线实际上并不存在,我只是用它们来布置我认为应该是行和列的位置.

...this is how the rendered window looks on my machine (Windows 7, Python 2.7.3). Note that the green lines aren't actually there, I'm just using them to lay out what I assume should be the positions of the rows and columns.

到目前为止一切顺利.现在我想将 entNamewidth 设置为 60,以提供更多可见的空间来输入客户名称.因为我将它的 columnspan 设置为 2,并且因为每个小部件都为 sticky 设置了 tk.W选项,我希望增加 entName 的宽度除了加宽条目小部件本身并相应地加宽窗口之外没有任何影响.所有其他小部件相对于彼此的位置应该保持不变——对吗?

So far so good. Now I want to set the width of entName up to 60, to give more visible room to type in the customer name. Because I have its columnspan set to 2, and because every widget is set with tk.W for the sticky option, I'm expecting that increasing the width of entName would have no effect other than widening the entry widget itself, and widening the window accordingly. All other widgets' positions relative to each other should stay the same -- right?

没有.加宽 entName 也会导致 entID 被推离 lblID:

Nope. Widening entName also causes entID to be pushed out away from lblID:

那我错过了什么?为什么 entID 不保持原样?除了将第 2 行挡在自己的框架中之外,还有什么方法可以让它保持原位?

So what am I missing? Why doesn't entID stay where it was? And is there some way to make it stay in place, apart from blocking off row 2 into its own frame?

推荐答案

当你把入口小部件变大时,Tkinter 必须为第 0 列或第 1 列分配额外的空间.它根据每列的权重"来执行此操作具有.

When you make the entry widget bigger, Tkinter has to allocate extra space to either column 0 or column 1. It does this according to the "weight" each column has.

因为您没有明确设置权重,所以所有列的默认权重为零.然而,在你的情况下,有些东西必须成长.如果没有列有权重,Tkinter 会选择第一列.这就解释了为什么 entID 会被推过去——第一列被加宽以容纳小部件条目小部件.

Because you haven't explicitly set a weight, all columns get the default weight of zero. In your case, however, something has to grow. Tkinter chooses the first column if no columns have weight. This explains why entID gets pushed over -- the first column is made wider to accommodate the widget entry widget.

如果给第 1 列的权重为 1,则任何额外的空间都将分配给该列,使第 0 列的宽度刚好足以容纳限制该列的任何内容(即:任何不跨列的小部件).您可以使用 grid_columnconfigure 命令执行此操作.

If you give column 1 a weight of 1, any extra space will be allocated to that column, leaving column 0 to be just wide enough to hold whatever is constraining that column (ie: any widgets that don't span columns). You do this with the grid_columnconfigure command.

例如:

winAddNew.grid_columnconfigure(1, weight=1)

注意权重可以是任意正整数;它代表一个比率.因此,例如,如果您将第 0 列设置为 1,将第 1 列设置为 2,则第 1 列将获得两倍于第 0 列的额外空间量.

Note that weight can be any positive integer; it represents a ratio. So, for example, if you set column 0 to 1 and column 1 to 2, column 1 will be given twice the amount of extra space that column 0 is given.

Tkinter 在记录这种行为方面做得很差.最终文档在 Tcl/Tk 手册页中.具体请参见网格手册页中的网格算法.

Tkinter does a poor job of documenting this behavior. The definitive documentation is in the Tcl/Tk man pages. Specifically, see Grid Algorithm in the grid man page.

这篇关于Python Tkinter 网格几何粘性设置无效(?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆