Python Forum
list, int and .grid() come on!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list, int and .grid() come on!
#6
I believe "grid" is the layout manager "grid" from tk. I don't know what kind of widgets are placed in "piece_counter" so I guessed Entry.
import tkinter as tk

COLUMNS = 7
ROWS = 7
root = tk.Tk()

piece_counter = []
for c in range(COLUMNS):
    for r in range(ROWS-1, -1, -1):
        entry = tk.Entry(root, width=4)
        entry.grid(row=r, column=c, padx=0, pady=0, sticky=S)
        piece_counter.append(entry)
After this code executes you should have 49 Entry widgets in piece_counter[] and there should be 49 entry widgets arranged in a grid pattern in the root window.

.grid() tells tk where to put the widget using the grid layout. The return value from grid() is None. You should not string widget creation and placement commands together if you want to keep the widget handle.
# entry == None after this call
entry = tk.Entry(root, width=4).grid(row=r, column=c, padx=0, pady=0, sticky=S)

# entry has the Entry widget handle afer this call
entry = tk.Entry(root, width=4)
entry.grid(row=r, column=c, padx=0, pady=0, sticky=S)
To call .grid() you need a widget handle. This code crashed because you didn't make any widgets. piece_counter is an empty list, so this results in an index error
piece_counter[piece_index].grid(...
Davy_Jones_XIV likes this post
Reply


Messages In This Thread
list, int and .grid() come on! - by Davy_Jones_XIV - Jan-23-2021, 08:44 PM
RE: list, int and .grid() come on! - by BashBedlam - Jan-23-2021, 08:56 PM
RE: list, int and .grid() come on! - by buran - Jan-23-2021, 09:04 PM
RE: list, int and .grid() come on! - by BashBedlam - Jan-23-2021, 09:16 PM
RE: list, int and .grid() come on! - by deanhystad - Jan-23-2021, 10:05 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020