简体   繁体   English

Tkinter Grid Columnspan被忽略

[英]Tkinter Grid Columnspan ignored

Consider the following python script 考虑以下python脚本

#!/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()

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'设置什么值,两个标签都占据窗口的一半,无论是否调用Grid.columnconfigure或在grid()中使用了sticky参数。

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. 除非我忽略了某些内容,否则我会认为设置columnpan将迫使第二个标签的宽度是第一个标签的'SOME_VALUE_HERE'倍。

Have I misunderstood how grid works? 我是否误解了网格的工作原理? How would I go about achieving this behavior? 我将如何实现这种行为?

When you put something in column 1 with a columnspan of two (or more) that means it will be in column 1 and column 2 (etc). 当您在第1列中放入两栏(或更多)的列时,这意味着它将在第1栏和第2列(等)中。 However, if there is nothing controlling the width of a column, that column will have a width of zero. 但是,如果没有什么控制列的宽度,则该列的宽度为零。 You need to force column 2 to have a widtheither by putting something in there, giving it a minsize, or forcing uniform columns. 您需要通过在其中放置一些东西,使其具有最小尺寸或强制使用统一的列来强制第2列具有一定的宽度。

When I look at your code, I can't guess how wide you think column 2 should be, and neither can the computer. 当我查看您的代码时,我无法猜测您认为第2列应该有多宽,计算机也不能。

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    |
+---------------+---------------++++

To provide strict proportional column width, you have to use the uniform option of columnconfigure . 要提供严格的比例列宽,您必须使用columnconfigureuniform选项。 uniform take an arbitrary value to designate the group of column that share this proprotions, weight argument is used to properly handle widget resizing. uniform采用任意值来指定共享此推进力的列组, weight参数用于正确处理窗口小部件调整大小。

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")

Note that with onyly this two labels, you could achieve the same layout by adjusting the width of column 1. Differences will occurs still while you populate column 2,3,4... 请注意,仅使用这两个标签,就可以通过调整第1列的宽度来实现相同的布局。在填充第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")

I had a similar problem only to discover that the elements are limited by the widest widget. 我有一个类似的问题,只是发现元素受最宽的小部件限制。 We can safely say that Tkinter is configured to make your app uniform in that it should be a regular repeating square/triangular structure. 可以肯定地说,Tkinter被配置为使您的应用程序统一,因为它应该是规则的重复正方形/三角形结构。 Solution to override default options. 覆盖默认选项的解决方案。

  1. With the Tkinter's automatic optimization in mind, play with the width and height of largest widget (grid box) and relate the other boxes to it proportionally. 牢记Tkinter的自动优化功能,可以处理最大的小部件(网格框)的宽度和高度,并按比例将其他框与之关联。
  2. Using the above method use columnspan to adjust the width. 使用上述方法,请使用columnspan来调整宽度。
  3. Configure the widths by use of columnconfigure() 使用columnconfigure()配置宽度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM