I am trying to create a button with a fixed size. So, I created a Frame (grid) with the size I want and then a child Button (pack) inside of the Frame that takes all the space.
Here is a minimal example:
window = tk.Tk() frame1 = tk.Frame(window, bg="#ffffff", height=50, width=100) frame1.grid(row=0, column=0) frame1.grid_propagate(0) #frame1.propagate(False) # <----- This line makes it work. button1 = tk.Button(frame1, bg="#000000") button1.pack(expand=True, fill=tk.BOTH)
Without the commented line above, I expected that it would work (having a rectangle of the specified size), but I get a small square. Why is it not working?
I was not able to find documentation on that .propagate()
function. I only found about the grid_propagate()
and pack_propagate()
functions.
Advertisement
Answer
Right now you have 2 containers: window
and frame1
. When you use frame1.grid(...)
, you actually tell the master of frame1
, which is window
that it should use the grid manager. When you use button1.pack(...)
, you tell the button’s master (frame1
) that it should use the pack manager inside itself.
So when you use frame1.grid_propagate(...)
, you tell the grid manager (that doesn’t manage the widgets inside frame1
), that it should do something, so it ignores you.