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!
#1
Hey all,

I am stuck on this error that I thought I understood, but I am struggling, so I decided to reach out to the community.

The code is:
from tkinter import *

piece_counter = []
piece_index = 0

for c in range(7):
        for r in range(6, 0, -1): 
                piece_counter = piece_counter[piece_index].grid(row    = r,
                                                                column = c,
                                                                padx   = 0,
                                                                pady   = 0,
                                                                sticky = S)
When I execute the code, I am given an AttributeError:

Error:
AttributeError: 'int' object has no attribute 'grid'
Can someone offer a hint as to what I am doing wrong please.
Reply
#2
What is it that you are expecting you code to do? Are you trying to create buttons, labels or some sort of widgets or just initialize a list?
Reply
#3
(Jan-23-2021, 08:56 PM)BashBedlam Wrote: What is it that you are expecting you code to do? Are you trying to create buttons, labels or some sort of widgets or just initialize a list?

Hi,

Just initialize list for my game.
Reply
#4
Actually this code will give you

Error:
Traceback (most recent call last): File "***", line 8, in <module> piece_counter = piece_counter[piece_index].grid(row = r, IndexError: list index out of range
that is because piece_counter is empty list and there is no element at index 0.
please, post actual code you run as well as full traceback you get (not just the last line).

also note that start imports are generally discouraged
better
import tkinter as tk and then use the tk to fully reference whatever you used from imported module.
Davy_Jones_XIV likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Jan-23-2021, 08:59 PM)Davy_Jones_XIV Wrote: Just initialize list for my game.
Sooooo... Something like this?

piece_counter = []
piece_index = 3

for p in range (piece_index) :
	for c in range (7) :
		for r in range (6, 0, -1) :
			grid = {'row': r, 'column': c, 'padx': 0, 'pady': 0, 'sticky': 'S'} 
			piece_counter.append (grid)

for element in piece_counter :
	print (element)
Davy_Jones_XIV likes this post
Reply
#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


Forum Jump:

User Panel Messages

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