0

I am trying to place a button on a frame for an application that am working on... But the frame disappears when I use pack or grid

from Tkinter import *
root=Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.geometry(str(width)+'x'+str(height))
frame=Frame(root,width=500, height=500, bg='black')
but1=Button(frame,text='qwe')
but1.grid()
frame.grid()
root.mainloop()

And if I use place, even worse, both of them disappear...I dont see the Frame and the Button

from Tkinter import *
root=Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.geometry(str(width)+'x'+str(height))
frame=Frame(root,width=500, height=500, bg='black')
but1=Button(frame,text='qwe')
but1.place()
frame.place()
root.mainloop()

but when I use pack_propagate(0), I can see them both...

from Tkinter import *
root=Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.geometry(str(width)+'x'+str(height))
frame=Frame(root,width=500, height=500, bg='black')
but1=Button(frame,text='qwe')
but1.pack()
frame.pack_propagate(0)
frame.pack()
root.mainloop()

My Questions are,

  1. What does pack_propagate(0) mean ?
  2. Why is the frame behaving weird with and without pack_propagate(0) ?
  3. What is the equivalent of pack_propagate(0) for GRID and PLACE ?
Franky
  • 23
  • 5

1 Answers1

2

What does pack_propagate(0) mean ?

pack_propagate(0) tells tkinter to let the parent control its own size, rather than letting it's size be determined by the children of the widget when using pack to manage the children.

Why is the frame behaving weird with and without pack_propagate(0)

I don't see any weird behavior. You'll have to be more precise about what you mean by "weird". It's behaving as documented. place will not cause the children to determine the size of the parent. If you don't give the frame a size, it's size will be 1x1 (ie: a single pixel). And, if the frame is invisible, any widgets inside the frame will also be invisible.

When you use pack or grid, the frame will try to shrink to fit its children. It doesn't disappear, you just can see it because its children are on top of it. If you add some padding, you'll see the frame.

For example, in the first block, change where you call grid on but1 to be like the following code and your black frame will appear:

but1.grid(padx=20, pady=20)

This is one of the reasons why place is rarely the right choice. Both pack and grid do a really great job of making sure all widgets are just the right size based on what is inside them.

What is the equivalent of pack_propagate(0) for GRID and PLACE

For grid it's grid_propagate. There's no equivalent for place because that's the defined behavior of place -- it never affects the size of the containing widget.

For the vast majority of cases, you should not use pack_propagate(0), grid_propagate(0), or place. When you use grid and pack properly, your gui will be the right size on every platform you run it on, no matter what the resolution and no matter what fonts the user has loaded.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685