Tkinter, unresolved attribute

I found this example and want to use it in a script. The problem is the unresolved attribute. I tried using ‘grid’, but that doesn’t help. Two questions, Why does the lower case ‘grid’ work in the next to last line? Where did the ‘Grid’ come from?

Thanks

from tkinter import *


# Create & Configure root
root = Tk()
Grid.rowconfigure(root, 0, weight=1)
Grid.columnconfigure(root, 0, weight=1)

# Create & Configure frame
frame=Frame(root)
frame.grid(row=0, column=0, sticky=N+S+E+W)

# Create a 5x10 (rows x columns) grid of buttons inside the frame
for row_index in range(5):
    Grid.rowconfigure(frame, row_index, weight=1)
    for col_index in range(10):
        Grid.columnconfigure(frame, col_index, weight=1)
        btn = Button(frame)  # create a button inside frame
        btn.grid(row=row_index, column=col_index, sticky=N+S+E+W)

root.mainloop()

Output:
Unresolved attribute reference 'rowconfigure' for class 'Grid'

I’d be interested to know where you found this example, it doesn’t seem right. Firstly, Grid.rowconfigure doesn’t seem to be a thing. Tk.rowconfigure (and Frame.rowconfigure) do seem to exist, so I assume that’s what was meant.

Note that instead of doing Tk.rowconfigure(root, ...) you can do root.rowconfigure(...).

This is a running version of the code. I’m not experienced enough with tkinter to comment on whether this is a “good” way of doing whatever it is meant to do.

from tkinter import *


# Create & Configure root
root = Tk()
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

# Create & Configure frame
frame=Frame(root)
frame.grid(row=0, column=0, sticky=N+S+E+W)

# Create a 5x10 (rows x columns) grid of buttons inside the frame
for row_index in range(5):
    frame.rowconfigure(row_index, weight=1)
    for col_index in range(10):
        frame.columnconfigure(col_index, weight=1)
        btn = Button(frame)  # create a button inside frame
        btn.grid(row=row_index, column=col_index, sticky=N+S+E+W)

root.mainloop()

Why does the lower case ‘grid’ work in the next to last line?

That’s a method on the Button class (and other widgets). See The Tkinter Grid Geometry Manager.

Where did the ‘Grid’ come from?

I don’t know. From what I can tell I don’t think it’s meant for you to use in your code and is instead used internally to implement the grid method.

Grid is a class in Tkinter that is a layout. Layouts are responsible for the place of widgets in a window/frame etc. Each component “gets” the layout from its master.

In this case the master is frame, so btn gets the grid from its master, frame. On the second line btn tells the master it wants to be in the row row_index and the column column_index. sticky tells it how to place it inside the grid cell, in this case in the middle, it sticks to each side.

By Leonard Dye via Discussions on Python.org at 14Sep2022 17:21:

I found this example and want to use it in a script. The problem is the
unresolved attribute. I tried using ‘grid’, but that doesn’t help. Two
questions, Why does the lower case ‘grid’ work in the next to last
line? Where did the ‘Grid’ come from?

Have a read of this:
https://tkdocs.com/shipman/grid-config.html
and the preceeding sections.

The rowconfigure method and friends are methods of individual widgets,
not standalone things.

As Menno Hölscher says, “grid” is only of the layout schemes available
for container widgets like frames. So in your code where you go:

 # Create & Configure frame
 frame=Frame(root)
 frame.grid(row=0, column=0, sticky=N+S+E+W)

now the frame uses a grid layout to place things within it. You can then
use the rowconfigure or columnconfigure methods on that frame.
Untested example:

 frame.rowconfigure(0, weight=1)
 frame.columnconfigure(0, weight=1)

Notice that I’ve dropped the root parameter. That is because the
method descriptions you’ve used (Grid.rowconfigure) are the bare
function. When you call a method on an object, the object gets placed
into the method call, so:

 frame.rowconfigure(0, weight=1)

effectives makes this call:

 Grid.rowconfigure(frame, 0, weight=1)

You want the former, not the latter.

Cheers,
Cameron Simpson cs@cskk.id.au

Thanks for the explaination! Since we configured the frame with grid, we can now load our widget into row & column. We could put a scroll bar into the frame and then grid the scroll box so we can put the group of buttons into the scroll box. Concepts are so inportant.
So much to learn. :slight_smile: