开发者

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.


This should do the job: ROOT.geometry("640x480")

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜