为什么在网格中使用相同权重时这些按钮的大小不一样?
class Screen(tk.Tk):
def __init__(self):
super().__init__()
f = tk.Frame(self)
f.pack(fill=tk.BOTH, expand=True)
f.grid_columnconfigure(0, weight=1)
f.grid_columnconfigure(1, weight=1)
f.grid_columnconfigure(2, weight=1)
f.grid_columnconfigure(3, weight=1)
l1 = tk.Label(f, text='Text')
l1.grid(row=0, column=0, columnspan=4, sticky='nsew')
l2 = tk.Label(f, text='Text', anchor='w')
l2.grid(row=1, column=0, sticky='sw')
self.e1 = tk.Entry(f)
self.e1.grid(row=2, column=0, columnspan=3, sticky='nsew')
self.b1 = tk.Button(f, text='B1')
self.b1.grid(row=2, column=3, sticky='nsew')
l3 = tk.Label(f, text='Text', anchor='w')
l3.grid(row=3, column=0, sticky='sw')
self.e2 = tk.Entry(f)
self.e2.grid(row=4, column=0, ipady=2, columnspan=3, sticky='nsew')
self.b2 = tk.Button(f, text='B2')
self.b2.grid(row=5, column=2, sticky='nsew')
self.b3 = tk.Button(f, text='B3')
self.b3.grid(row=5, column=3, sticky='nsew', command=self.test)
self.update_idletasks()
self.geometry(f'500x{self.winfo_reqheight()}')
self.resizable(False, False)
def test(self):
a = self.b1.winfo_width()
b = self.b2.winfo_width()
c = self.b3.winfo_width()
print(a, b, c)
输出:111 110 111
按钮 B1
和 B3
位于网格的第 3 列,B2
位于第 2 列。确保它们的唯一方法如果我将其 width
配置设置为 width=10
之类的值,则所有尺寸都相同。
所需的布局:
+---------------------------------------------------------+
| L1 |
+--------------+------------+--------------+--------------+
| L2 | | | |
+--------------+------------+--------------+--------------+
| E1 | B1 |
+--------------+------------+ -------------+--------------+
| L3 | | | |
+--------------+------------+--------------+--------------+
| E2 | |
+--------------+------------+ -------------+--------------+
| | | B2 | B3 |
+--------------+------------+--------------+--------------+
尝试使列的比例尽可能接近均匀。但布局是5行4列的网格。顶部标签 L1
将跨越第一行中的所有列。两个条目小部件 E1
和 E2
将跨越各自行中的前 3 列。由于某种原因,B2
接收的大小较小。
编辑:我能确定的是,Entry
小部件会导致加权问题,但我不知道为什么。 Bryan Oakley 回答了同一主题 here但我很困惑为什么小部件上的默认宽度不会被网格管理器的weight关键字覆盖。
最佳答案
Question: Why is it that these buttons are not the same size when using equal weights in the grid?
仅加权,未计算。这取决于使用的小部件和列跨度。
您正在使用sticky='nsew'
总体而言,更改为 sticky='ew'
如果你不想增长到sn
.
一种可能的选择是使用不同的 weight=
值,给网格管理器一个提示,您想要哪个小部件更加权,例如:
f.grid_columnconfigure(0, weight=1)
f.grid_columnconfigure(1, weight=1)
f.grid_columnconfigure(2, weight=2)
f.grid_columnconfigure(3, weight=4)
<小时/>
另一个选择是使用 .grid_columnconfigure(c, minsize=<minsize>)
.
这迫使网格管理器遵守minsize=
对于一个列。
Source:
tkinter.__init__: def grid_columnconfigure(...
Valid resources are:
- minsize (minimum size of the column),
- weight (how much does additional space propagate to this column)
- pad (how much space to let additionally)
但结果也是不可预测的,充其量在固定窗口大小下也是如此。
<小时/>要使其在调整窗口大小时起作用,请绑定(bind) <Configure>
事件至Frame
和
设置相同的计算 minsize=
对于每一列。
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
f = tk.Frame(self)
f.pack(fill=tk.BOTH, expand=True)
f.bind('<Configure>', self.on_configure)
...
def on_configure(self, event):
w = event.widget
columns = w.grid_size()[0]
minsize = w.winfo_width() // columns
for c in range(columns):
w.grid_columnconfigure(c, minsize=minsize)
if __name__ == "__main__":
App().mainloop()
使用 Python 测试:3.5 - 'TclVersion':8.6 'TkVersion':8.6
关于python - 同等权重的网格管理器中按钮大小不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58400184/