views:

30

answers:

2

I'm creating a GUI using Tkinter/ttk in Python 2.7, and I'm having an issue where a Frame will resize itself once a widget is placed inside of it. I am new to Python and haven't used Tkinter before.

example:

ROOT = Tk()
FRAME = ttk.Frame(ROOT, width=300, height=300, relief='groove')
FRAME.grid()
ROOT.mainloop()

will produce a grooved frame 300x300, if i place a widget inside it like so:

ROOT = Tk()
FRAME = ttk.Frame(ROOT, width=300, height=300, relief='groove')
BUTTON = ttk.Button(FRAME, text="DON'T READ THIS TEXT")
FRAME.grid()
BUTTON.grid()
ROOT.mainloop()  

the frame will shrink down to fit the button. Any way to force the frame not to resize?

A: 

if you want to add padding, then use widg.grid(ipadx=..., ipady=..., padx=..., pady=...)

otherwise, you will need to add more context on what layout you're trying to achieve

Lie Ryan
I don't want to add padding, I want to see if there is a way to force the frame to stay at the size I told it to be, what is the point of the size and width option if it doesn't hold?
@user481875: the height and width are only relevant for .place() geometry manager. The idea of using .pack() or .grid() window manager is that you shouldn't need to care about the actual position or size of widgets, as the geometry manager will figure that out.
Lie Ryan
@user481875: the point of width and height options are to give the geometry managers hints as to the preferred size. The geometry manager may choose to resize them based on how you configure it, but it will try to honor the requested size if at all possible.
Bryan Oakley
+1  A: 

To force the frame to keep its original dimensions is to turn "geometry propagation" off. In your case you would call FRAME.grid_propagate(False).

Speaking as someone with over 15 years of Tk experience, might I suggest that you almost certainly don't need this feature. Tk's ability to "shrink to fit" is really great, and makes it really easy to create GUIs with proper resize behavior. Once you start turning geometry propagation off you'll find you'll either have GUIs with bad resize behavior, or you'll spend a lot of time tweaking sizes and widget placement.

Bryan Oakley
Thanks for the explanation