在 Python Tkinter 中使用网格布局拉伸框架 [英] Stretching frames using grid layout in Python Tkinter

查看:59
本文介绍了在 Python Tkinter 中使用网格布局拉伸框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 2.6.7 和 Tkinter 进行伸展工作.我希望下面的代码可以将第一个按钮拉伸到第二个按钮的宽度,但两个按钮的宽度都只有它们适合其文本所需的宽度.

I'm trying to get stretching to work using Python 2.6.7 with Tkinter. I'd expect the below code to stretch the first button to the width of the second, but both buttons are only as wide as they need to be to fit their text.

#!/usr/bin/python
from Tkinter import *

win = Frame()
win.grid(sticky=N+S+E+W)

inner_a = Frame(win)
inner_a.grid(row=0, column=0, sticky=N+E+W)
inner_b = Frame(win)
inner_b.grid(row=1, column=0, sticky=S+E+W)

Button(inner_a, text='1').grid(row=0, column=0, sticky=E+W)
Button(inner_b, text='Some long text').grid(row=0, column=0, sticky=E+W)

win.mainloop()

据我了解,win中的单列会扩展到它所包含的最大事物的宽度,即inner_b的宽度,然后是<的宽度code>inner_a,然后第一个按钮的位置将成为第二个按钮的位置.

By my understanding, the single column in win will expand to the width of the largest thing it contains, ie the width of inner_b, and then the width of inner_a, and thence of the first button, will be that of the second button.

实际上,发生的事情如下;第一个按钮的宽度仅足以包含1",而没有第二个按钮那么宽.

Actually, what happens is the below; the first button is only wide enough to contain "1", not as wide as the second button.

我需要做什么才能让第一个按钮扩大第二个按钮的大小?

What do I need to do to get the first button to expand the size of the second?

推荐答案

如果您希望小部件在网格中排列,首先要做的是确保它们具有相同的父级.如果所有小部件的大小相同或者您只使用一列,则这不是绝对必要的.

If you want widgets to line up in a grid, the first thing to do is make sure they have the same parent. This isn't strictly necessary if all the widgets are the same size or you are only using one column.

您需要做的另一件事是给您的列赋予权重.您的代码中发生的情况是小部件正在扩展以填充该列,但该列未扩展以填充母版.如果你给它的权重为 1,它就会.您需要对 inner_a.columnconfigure(1, weight=1) 做类似的事情,然后对 inner_b 做同样的事情.

Another thing you need to do is give your column a weight. What is happening in your code is that the widget is expanding to fill the column, but the column isn't expanding to fill the master. If you give it a weight of 1 it will. You ned to do something like inner_a.columnconfigure(1, weight=1), and then do likewise for inner_b.

这篇关于在 Python Tkinter 中使用网格布局拉伸框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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