You are correct about the width and height not being honored, but not about the background color. The background color is unaffected, but you can't see the background because the background fits precisely around the label. If you add padding to the label when you pack it you'll see the background.
As for the width and height... this is one of the great features of Tkinter. By default, a container widget expands or collapses to be just big enough to hold its contents. Thus, when you call pack
, it causes the frame to shrink. This feature is called geometry propagation.
For the vast majority of applications, this is the behavior you want. For those rare times when you want to explicitly set the size of a container you can turn this feature off. To turn it off, call either pack_propagate
or grid_propagate
on the container (depending on whether you're using grid or pack on that container), giving it a value of False
.
Using your code as an example, you would do:
page1.pack_propagate(False)
My recommendation is to not do that, and instead learn how to work with geometry propagation. It will make your GUIs behave better when the user resizes the windows, and you won't be spending time trying to calculate the right size for a window. Let Tkinter do that for you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…