How can i change frame widget width in python How can i change frame widget width in python tkinter tkinter

How can i change frame widget width in python


The width is returning 1 because the window hasn't been drawn yet. The actual width depends on the window being drawn since the actual width depends on many factors which can't be known before the window is actually drawn.

If you call root.update() before calling Frame.winfo_width() to force the window to be drawn, you will see it displaying the actual value.

As for how to change the width, that question is too broad to answer. Normally it's not wise to directly set the width of a frame. Tkinter by default will automaticaly resize a frame to fit its children. So, one way to make the frame wider is to add more widgets.

The width can also depend on how it is added to the display - whether you're using pack or grid or place, and how you have configured them. So, another way to make the frame wider is to use non-default options that cause the frame to grow or shrink to fit the space given to it.

If you want to specify an explicit size and ignore tkinter's automatic resizing, you can do that by turning off geometry propagation and then setting the width and height parameters for the frame. Depending on whether you're using grid or pack, you can call grid_propagate or pack_propagate with a False argument to disable propagation (place doesn't support geometry propagation).

Note that turning off geometry propagation is usually the least desirable solution because it requires you to do a lot more work to create a responsive UI. The best way to design GUI with tkinter is to focus on the size of the inner widgets and let tkinter compute the most efficient size for frames and the window itself.


As the others have pointed out how to set a static size frame using grid_propagate() I will show you how to set up your frame to resize automatically.

You need to tell the row and column to expand that the frame is in. This is done with columnconfigure() and rowconfigure(). Then you need to tell the frame to stick to all sides with sticky='nsew'. Adding widgets to the frame is no different then any other container. Simply tell the widget to be in the frame.

One potention issue I see is you are overwriting Frame() on this line: Frame = LabelFrame(root, text="Test", width = 200). This is a good example why you should not use import *. Instead do import tkinter as tk and use the tk. prefix for anything that needs it.

Example:

import tkinter as tkroot = tk.Tk()root.geometry('400x300')root.columnconfigure(0, weight=1)root.rowconfigure(0, weight=1)frame = tk.LabelFrame(root, text='Test', width=200)frame.grid(row=0, column=0, sticky='nsew')label = tk.Label(frame, text='label').grid(row=0, column=0)root.mainloop()

Results:

enter image description here

Update:

If you do want something static make sure you define both height and width. If you only define one or the other then you will not see the frame in the window.

For a testable example for a static frame size:

import tkinter as tkroot = tk.Tk()root.geometry('400x300')root.rowconfigure(0, weight=1)root.columnconfigure(0, weight=1)frame = tk.LabelFrame(root, text='Test', height=200, width=200)frame.grid(row=0, column=0)frame.grid_propagate(False)label = tk.Label(frame, text='label').grid(row=0, column=0)root.mainloop()

Results:

enter image description here


Your frame can propagate on the grid based on the widgets on it, and not have fixed dimensions.

The output of 1 is due there being nothing on the Frame other than an empty Label. (It would still show 1 if there was no Label)

To get the output as 200, set the grid_propagate flag to False (only after setting your height and widht parameters), as follows:

frame = Frame(..., width=200)frame.grid(row=0, column=0)frame.grid_propagate(False)