Reputation: 53
win=Tk()
level1=matrixmaker(4)
def display(x,y):
if z["text"] == " ":
# switch to Goodbye
z["text"] = level1[x][y]
else:
# reset to Hi
z["text"] = " "
for x in range(0,4):
for y in range(0,4):
z=Button(win,text=" ", command=display(x,y))
z.grid(row=x,column=y)
I have this code but I don't know how to get the display function to work. How can I call the button and change the text without it having a hardcoded variable name?
Upvotes: 0
Views: 51
Reputation: 5289
You can't assign the command to the button with the called function (display(x, y)
) because this assigned what this function returns (None
) to the button's command. You need to assign the callable (display
) instead.
To do this and pass arguments you'll need to use a lambda:
z = Button(win, text='')
z['command'] = lambda x=x, y=y, z=z: display(x, y, z)
z.grid(row=x, column=y)
Also, you'll want to change your display()
function to accept another argument z
so that the correct button gets changed (and correct the indentation):
def display(x,y,z):
if z["text"] == " ":
# switch to Goodbye
z["text"] = level1[x][y]
else:
# reset to Hi
z["text"] = " "
Upvotes: 4