Home > Mobile >  Tkinter grid_columnfigure not working in Frame widget
Tkinter grid_columnfigure not working in Frame widget

Time:07-03

I'm using .grid() to position things into a Frame. However, when I use .grid_columnconfigure() to make the column within that Frame resize to the window, it does absolutely nothing. To make matters more confusing, replacing menu.grid_columnconfigure() with root.grid_columnconfigure() indeed makes the column resize - so it seems as though the Label isn't being packed into the Frame, but root instead.

Code:

from tkinter import *

root = Tk()
root.geometry("300x300")

menu = Frame(root)
menu.columnconfigure(0,weight=1)
l = Label(menu,text="Main menu")

menu.grid()
l.grid(column=0)

CodePudding user response:

When you call menu.columnconfigure(0, weight=1), it affects the children that are added to the menu, not the menu in the root window.

If you want the menu frame to fill the window, you need to configure the rows and columns in its parent, and also set the sticky attribute so that it fills the space allocated to it.

If you're trying to affect the children of the inner frame, it is working as documented. It doesn't look like it's working because menu is shrinking to fit the label. The first column does indeed take up all of the extra space in menu, but menu doesn't have any extra space because of how it's added to the root window.

You can see this fairly easily by giving menu a distinctive color. You'll see that in your original code the frame is invisible because it shrinks to fit its contents.

screenshot of original code with red menu

When I configure the rows and columns of the root window, and then request that the menu fill the root window, you can now see that column 0 inside menu is indeed taking up all of the space.

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
...
menu.grid(sticky="nsew")

screenshot after configuring root window

  • Related