Python Forum
list, int and .grid() come on! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: list, int and .grid() come on! (/thread-32148.html)



list, int and .grid() come on! - Davy_Jones_XIV - Jan-23-2021

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.


RE: list, int and .grid() come on! - BashBedlam - Jan-23-2021

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?


RE: list, int and .grid() come on! - Davy_Jones_XIV - Jan-23-2021

(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.


RE: list, int and .grid() come on! - buran - Jan-23-2021

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.


RE: list, int and .grid() come on! - BashBedlam - Jan-23-2021

(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)



RE: list, int and .grid() come on! - deanhystad - Jan-23-2021

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(...