简体   繁体   中英

Frame resizing issue in Tk/ttk python

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?

To force the frame to keep its original dimensions 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.

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

This works for me:

app = Application()
app.master.title('Example')
app.master.geometry('640x480')
app.mainloop()

Apllication is Frame in my case with grid layout. It resizes to size of master window, so we need to change size of master window.

这应该可以完成: ROOT.geometry("640x480")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM