4
votes

In Python3/tkinter is there a simple way to set the size of a ttk.frame as being relative to its parent window size and ensure that it will adjust properly (with all its widgets) on window resize?

As a bit of context, I am trying to pack() a ttk.frame a few pixels smaller than the root window width.

1
There is no single answer to this question. Making a frame a size relative to the parent can be accomplished many ways. It depends on what is in the frame, what is in the parent frame, how these frames use pack, grid or place, how you want extra space to be handled, etc. This question is simply too broad as written. - Bryan Oakley
he does especify the use of pack though - Mixone

1 Answers

4
votes

If you know the size you want and have it you do:

root = # Your root window
myFrame = ttk.Frame(root, height=desiredHeight, width=desiredWidth)
myFrame.pack()

So if you want it relative to the root:

root = # Your root window
rootHeight = root.winfo_height()
rootWidth = root.winfo_width()
# Say you want it to be 20 pixels smaller
myFrame = ttk.Frame(root, height=rootHeight-20, width=rootWidth-20)
myFrame.pack()
# OR
myFrame = ttk.Frame(root) # No set dimensions
myFrame.pack(padx=20, pady=20)
# This will make it have a padding of 20 pixels on height and width,
# with respect to parent rather than itself