How to prevent Tkinter labelframe size changes when an empty label is inserted How to prevent Tkinter labelframe size changes when an empty label is inserted tkinter tkinter

How to prevent Tkinter labelframe size changes when an empty label is inserted


That problem always was interesting to me. One way I found to fix it is by using the place method instead of grid:

import Tkinterform = Tkinter.Tk()errorArea = Tkinter.LabelFrame(form, text=" Errors ", width=250, height=80)errorArea.grid(row=2, column=0, columnspan=2, sticky="E", \             padx=5, pady=0, ipadx=0, ipady=0)errorMessage = Tkinter.Label(errorArea, text="")# 1) 'x' and 'y' are the x and y coordinates inside 'errorArea'# 2) 'place' uses 'anchor' instead of 'sticky'# 3) There is no need for 'padx' and 'pady' with 'place'# since you can specify the exact coordinateserrorMessage.place(x=10, y=10, anchor="w")form.mainloop()

With this, the label is placed in the window without shrinking the labelframe.


If you use a sticky value that sticks the widget to all four sides of its cell rather than just one side, it won't shrink when you put a small label widget in it.

Another option is to call errorArea.grid_propagate(False), which tells the grid area not to shrink or expand to fit its contents. This will often result in undesirable resize behavior, or at least require you to do a little extra work to get the right resize behavior.


Use the grid function immediately after declaring the Labelframe.

EX :

String_l = ttk.Labelframe(pw, text='String',width=100, height=408).grid(column=1, row=0, padx=4, pady=4,rowspan=2)