0

My code is the following:

import tkinter as tk

#setting up window.
root = tk.Tk()
root.title("CSV Maker")
root.geometry("600x300")

#setting up frames.
leftFrame = tk.Frame(root, bg="red", width=300, height=300)
rightFrame = tk.Frame(root, bg="blue", width=300, height=300)
#placing frames on window.
leftFrame.grid(row=0, column=0)
rightFrame.grid(row=0, column=1)

#setting up labels.
inputPathLabel = tk.Label(leftFrame, text="Input File Path:")

#placing labels on frames.
inputPathLabel.grid(row=0, column=0)
root.mainloop()

When I remove the label I get the following:

Without label

However when I leave the code as it is below (with a label), I get a completely different result. It seems as if the frame was resized to another size than the one that I selected and the color is gone. Why is this?

With label

1 Answer 1

2

That is simply how tkinter was designed to work. When you use pack or grid, frames (or any other widget) will shrink or expand to try to fit all of its contents.

99.9% of the time, this is the behavior you want. Tkinter is really good at making GUIs the appropriate size.

From the official documentation for grid:

The grid geometry manager normally computes how large a master must be to just exactly meet the needs of its slaves, and it sets the requested width and height of the master to these dimensions. This causes geometry information to propagate up through a window hierarchy to a top-level window so that the entire sub-tree sizes itself to fit the needs of the leaf windows. However, the grid propagate command may be used to turn off propagation for one or more masters. If propagation is disabled then grid will not set the requested width and height of the master window. This may be useful if, for example, you wish for a master window to have a fixed size that you specify.

From the documentation for pack:

The packer normally computes how large a master must be to just exactly meet the needs of its slaves, and it sets the requested width and height of the master to these dimensions. This causes geometry information to propagate up through a window hierarchy to a top-level window so that the entire sub-tree sizes itself to fit the needs of the leaf windows. However, the pack propagate command may be used to turn off propagation for one or more masters. If propagation is disabled then the packer will not set the requested width and height of the packer. This may be useful if, for example, you wish for a master window to have a fixed size that you specify.

Notice that place doesn't have the same behavior. From the place documentation:

Unlike many other geometry managers (such as the packer) the placer does not make any attempt to manipulate the geometry of the master windows or the parents of slave windows (i.e. it does not set their requested sizes). To control the sizes of these windows, make them windows like frames and canvases that provide configuration options for this purpose.

Sign up to request clarification or add additional context in comments.

3 Comments

Any idea where the background color went? And you're saying that the label/button/whatever size overrides the specified frame size?
@Joel: it's behind the label. Add some padding to see it (eg: inputPathLabel.grid(row=0, column=0, padx=20, pady=20))
You've been incredibly helpful. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.