Tkinter网格栏已被忽略 [英] Tkinter Grid Columnspan ignored

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

问题描述

考虑下面的python脚本

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

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参数,两个标签都占用一半的窗口。



除非我忽略了某些事情,否则我会认为设置栏范围会迫使第二个标签的'SOME_VALUE_HERE'时间与第一个标签一样宽。

我误解了网格如何工作?如何实现这种行为?

解决方案

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

  + ------------- -  + --------------- ++++ 
| 0 | 1 |||| < - 2,3,4空,0宽度
+ --------------- + --------------- + +++
| 1列宽| 4列宽|
+ --------------- + --------------- ++++

要提供严格的比例列宽,您必须使用 uniform 选项 columnconfigure uniform 取一个任意值来指定共享此proprotions的列的组, weight 参数用于正确处理小部件

  label1.grid(row = 0,column = 0)
label2.grid(row = 0, (宽度+ 1):
root.grid_columnconfigure(i,weight = 1,uniform =foo)

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


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

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().

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

To provide strict proportional column width, you have to use the uniform option of columnconfigure. uniform take an arbitrary value to designate the group of column that share this proprotions, 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")

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

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网格栏已被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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