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.