Stretching frames using a grid in Python Tkinter

I am trying to work with Python 2.6.7 using Tkinter. I expect the code below to stretch the first button to the width of the second, but both buttons are only as wide as necessary 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()

      

In my opinion, the single column in win

will expand to the width of the largest thing it contains, i.e. width inner_b

and then width inner_a

and from there the first button will be the second button.

What actually happens is that the first button is wide enough to contain a "1", not as wide as the second button.

Screen shot: two buttons stacked on top of each other, the first has the single character "1", the second the words "Some long text".  The bottom button is considerably wider than the top.

What do I need to do to make the first button increase the size of the second one?

+3


source to share


1 answer


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



Another thing you need to do is give your column weight. What's going on in your code is that the widget expands to fill the column, but the column doesn't expand to fill the master. If you give it a weight of 1, it will. You have to do something like inner_a.columnconfigure(1, weight=1)

and then do the same for inner_b

.

+8


source







All Articles