Tkinter Grid Columnspan 被忽略 [英] Tkinter Grid Columnspan ignored

查看:28
本文介绍了Tkinter Grid Columnspan 被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下 python 脚本

Consider the following python script

#!/usr/bin/env python
from Tkinter import Tk, Label

width = SOME_VALUE_HERE

root = Tk()
label1 = Label(root, text='1 columns wide')
label2 = Label(root, text='%i columns wide' % width)

label1.grid()
label2.grid(row=0,column=1,columnspan=width)

root.mainloop()

当我运行它时,无论为 'SOME_VALUE_HERE' 设置什么值,无论是否调用了 Grid.columnconfigure 或在 grid() 中使用了 sticky 参数,两个标签都占据了一半的窗口.

When I run this, no matter what value is set for 'SOME_VALUE_HERE', both labels take up half the window, regardless of whether or not Grid.columnconfigure is called, or the sticky parameter is used in grid().

除非我忽略了什么,否则我会认为设置列跨度会强制第二个标签的宽度是第一个标签的SOME_VALUE_HERE"倍.

Unless I've overlooked something, I would have thought that setting the columnspan would force the second label to be 'SOME_VALUE_HERE' times as wide as the first.

我是否误解了网格的工作原理?我将如何实现这种行为?

Have I misunderstood how grid works? How would I go about achieving this behavior?

推荐答案

默认情况下,空网格列是零宽度,所以你描述了下表.默认情况下,网格几何管理器会尝试优化应用程序使用的屏幕空间.它将整合所有约束并产生最合适的布局.

By default, empty grid column are zero width, so you described the following table. Grid geometry manager will by default try to optimize the screen real estate used by your application. It will integrate all the constraint and produce the fittest layout.

+---------------+---------------++++
|       0       |       1       |||| <-- 2,3,4 empty, 0 width
+---------------+---------------++++
| 1 column wide | 4 column wide    |
+---------------+---------------++++

要提供严格的比例列宽,您必须使用 columnconfigureuniform 选项.uniform 采用任意值来指定共享这些比例的列组,并且 weight 参数用于正确处理小部件的大小调整.

To provide strict proportional column width, you have to use the uniform option of columnconfigure. uniform takes an arbitrary value to designate the group of the column that share these proportions, and the weight argument is used to properly handle widget resizing.

label1.grid(row=0, column=0)
label2.grid(row=0,column=1, columnspan=width)
for i in range(width+1):
    root.grid_columnconfigure(i, weight=1, uniform="foo")

请注意,仅使用这两个标签,您可以通过调整第 1 列的宽度来实现相同的布局.当您填充第 2、3、4 列时,仍然会出现差异...

Note that with only these two labels, you could achieve the same layout by adjusting the width of column 1. Differences will occur still while you populate column 2,3,4...

label2.grid(row=0,column=1) #no columnspan
root.grid_columnconfigure(0, weight=1, uniform="foo")
root.grid_columnconfigure(1, weight=width, uniform="foo")

这篇关于Tkinter Grid Columnspan 被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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