Skip to content Skip to sidebar Skip to footer

Aligning Widgets Using Grid Between Multiple Tkinter LabelFrames

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 Tkin

Solution 1:

The short answer is: you can't do what you want. grid does 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_columnconfigure method 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).


Solution 2:

The easiest way I know to do what you want, is to change the make the Label text variables, and then, check the len(txt1) against len(txt2), and, set the width variable of both to the longest one. The following code is close. My knowledge is too limited to figure out where the extra space is coming from.

    txt1 = StringVar()
    txt2 = StringVar()
    lblWidth = IntVar()
    txt1 = "Hi"
    txt2 = "Longer label, shorter box"

    if (len(txt1) > len(txt2)):
        lblWidth = len(txt1)
    else:
        lblWidth = len(txt2)

    Label(win, text=txt1, width=lblWidth, anchor=W).grid(in_=frame_a)
    Label(win, text=txt2, width=lblWidth, anchor=W).grid(in_=frame_b, sticky=W)

Post a Comment for "Aligning Widgets Using Grid Between Multiple Tkinter LabelFrames"