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?
commandargument is a reference to a function, not the function itself. You have to make the argument intocommand=displaywith no arguments. See here: stackoverflow.com/questions/6920302/…command=lambda a=x,b=y:display(a,b)z- butzis the last button created inforloop.