I have a list of tkinter buttons that create a dialog window and allow the user to input text which will be the new text of the button. However I cannot seem to figure out how to make each button update itself. The code I have is below
myButtons = []
timeSlots = []
while startH < endH:
timeSlots.append(str(startH) + ":00")
timeSlots.append(str(startH) + ":15")
timeSlots.append(str(startH) + ":30")
timeSlots.append(str(startH) + ":45")
startH += 1
for x in range(len(timeSlots)):
Label(frame, text=timeSlots[x]).grid(row=x, column=0)
z = Button(frame, text="Nothing Scheduled", command=lambda: dialogMain(myButtons[x]))
z.grid(row=x, column=1)
myButtons.append(z)
The call to dialogMain opens the window for the user to input text and the argument is the button for it to update. The problem, of course, is that while all of the buttons appear in the correct spot, when I click on any button it will only update the last one which is the current value of x. Is it possible to store only the value of x,in place of a pointer to x, which I think is the problem, in the line
command=lambda: dialogMain(myButtons[x])
. I have tried adding a helper function which just takes in x, assigns it to a new variable and returns it as the index in the command call to dialogMain, but it seems like the function is not called until the button is clicked, leaving me with the same problem. If there is a way to reference the button itself that would work too, and I have tried something like the code below, but it still only updates the last button.
z = Button(frame, text="Nothing Scheduled")
z["command"] = lambda: dialogMain(z)
z.grid(row=x, column=1)
myButtons.append(z)
The window works fine and looks like, this, but only the bottom button will be updated