Reputation: 37
I am trying to create a grid of 12 buttons from a list of 3 items. Currently the code runs and create the 12 buttons in the grid, but each button is the last item in the list. Not sure how to store or save the list item for each row.
import tkinter as tk
root = tk.Tk()
testList = ['Boop','Yeet','Noop']
nrows = 4
ncols = 3
for r in range(nrows):
for c in range(ncols):
for i in testList:
tk.Button(text=i,padx=50, pady=50).grid(row = r, column=c)
root.mainloop()
Current output is a grid with 4 columns of 3 rows each with the word Noop on it.output
Upvotes: 0
Views: 698
Reputation: 484
So, your loop is actually running 36 times. For each r and c, it runs through all three words in the list and makes a button and replaces the previous one leaving you with the last word in all places. My best guess is that you were trying to do something like this:
import tkinter as tk
root = tk.Tk()
testList = ['Boop','Yeet','Noop']
nrows = 4
ncols = 3
for r in range(nrows):
for c in range(ncols):
tk.Button(text=testList[c],padx=50, pady=50).grid(row = r, column=c)
root.mainloop()
Upvotes: 0
Reputation: 368
Easy way to "debug", you can print out what's happening:
print("row " + str(r) + ", col " + str(c) + ", item " + i)
You get this output:
row 0, col 0, item Boop
row 0, col 0, item Yeet
row 0, col 0, item Noop
row 0, col 1, item Boop
row 0, col 1, item Yeet
row 0, col 1, item Noop
row 0, col 2, item Boop
row 0, col 2, item Yeet
row 0, col 2, item Noop
row 1, col 0, item Boop
row 1, col 0, item Yeet
row 1, col 0, item Noop
row 1, col 1, item Boop
row 1, col 1, item Yeet
row 1, col 1, item Noop
row 1, col 2, item Boop
row 1, col 2, item Yeet
row 1, col 2, item Noop
row 2, col 0, item Boop
row 2, col 0, item Yeet
row 2, col 0, item Noop
row 2, col 1, item Boop
row 2, col 1, item Yeet
row 2, col 1, item Noop
row 2, col 2, item Boop
row 2, col 2, item Yeet
row 2, col 2, item Noop
row 3, col 0, item Boop
row 3, col 0, item Yeet
row 3, col 0, item Noop
row 3, col 1, item Boop
row 3, col 1, item Yeet
row 3, col 1, item Noop
row 3, col 2, item Boop
row 3, col 2, item Yeet
row 3, col 2, item Noop
For every row, for every column in the row, you're setting the values of testList
, the last one being 'Noop'
.
I'm not sure what you want to do, as it is not clear in this question, but if you want every row to have the three items, you need to iterate over every row, then for every column, give it the corresponding value of testList
, which is testList[c]
:
for r in range(nrows):
for c in range(ncols):
print("row " + str(r) + ", col " + str(c) + ", item " + testList[c])
tk.Button(text=testList[c],padx=50, pady=50).grid(row = r, column=c)
The output becomes:
row 0, col 0, item Boop
row 0, col 1, item Yeet
row 0, col 2, item Noop
row 1, col 0, item Boop
row 1, col 1, item Yeet
row 1, col 2, item Noop
row 2, col 0, item Boop
row 2, col 1, item Yeet
row 2, col 2, item Noop
row 3, col 0, item Boop
row 3, col 1, item Yeet
row 3, col 2, item Noop
Edit: I was already writing my answer when jasonharper commented. Nothing stolen here!
Upvotes: 1