I’m trying to create a Tkinter layout that has labels and entry fields vertically aligned across multiple LabelFrame boxes.
Here’s some simplified code:
#!/usr/bin/python
from Tkinter import *
win = Frame()
win.grid(sticky=N+S+E+W)
frame_a = LabelFrame(win, text='Top frame', padx=5, pady=5)
frame_b = LabelFrame(win, text='Bottom frame', padx=5, pady=5)
frame_a.grid(sticky=E+W)
frame_b.grid(sticky=E+W)
for frame in frame_a, frame_b:
for col in 0, 1, 2:
frame.columnconfigure(col, weight=1)
Label(win, text='Hi').grid(in_=frame_a, sticky=W)
Label(win, text='Longer label, shorter box').grid(in_=frame_b, sticky=W)
Entry(win).grid(in_=frame_a, row=0, column=1, sticky=W)
Entry(win, width=5).grid(in_=frame_b, row=0, column=1, sticky=W)
win.mainloop()
The above code produces a window that looks like the below:

Whereas I’m looking to find some way of aligning the fields so the window looks more like this (with thanks to MS Paint):

I’ve played around with in_ arguments to grid(), but not achieved very much, and I can’t think of anything else to experiment with.
The short answer is: you can’t do what you want.
griddoes not manage its rows and columns across multiple containers.However, there are at least a couple ways to achieve the effect you are wanting. One way is to give the first column in each container an explicit, identical width. To do that you can use the
grid_columnconfiguremethod to give each column a minimum width.Another solution is to give each label an identical width, which effectively will set the width of the first column to be the same (assuming all columns in each container have the same weight).