How to stop Tkinter Frame from shrinking to fit its contents?

Question!

This is the code that's giving me trouble.

f = Frame(root, width=1000, bg="blue")
f.pack(fill=X, expand=True)

l = Label(f, text="hi", width=10, bg="red", fg="white")
l.pack()

If I comment out the lines with the Label, the Frame displays with the right width. However, adding the Label seems to shrink the Frame down to the Label's size. Is there a way to prevent that from happening?

By : Johnston


Answers

By default, tk frames shrink or grow to fit their contents, which is what you want 99% of the time. The term that describes this feature is "geometry propagation". There is a command to turn geometry propagation on or off.

Since you are using pack, the syntax would be:

f.pack_propagate(0)

or maybe root.pack_propagate(0), depending on which widget(s) you actually want to affect.

That being said, the vast majority of the time you should let tkinter compute the size. When you turn geometry propagation off your GUI won't respond well to changes in resolution, changes in fonts, etc. tkinter's geometry managers (pack, place and grid) are remarkably powerful. Learn to take advantage of that power.



What does your c# (or vb) code look like when you are creating the new category?



"Is llblgen just like codesmith?" : In many ways, yes. [Frans Bouma is active on SO, I'm sure he will have some input]

MyGeneration is a free tool that can create all of your CRUD plumbing.



This video can help you solving your question :)
By: admin