I have a loop that creates buttons from a list. I've been able to get it to create the buttons and return values when clicked. I'm running into an issue on changing the other button options such as enabling and disabling the button. The code that I have just disables the last button in the list regardless of which one was clicked. I have tried a number of fixes, listed below, but they either just don't plain work or they "work" but don't function as a normal button does in things like hover and enable/disable.
Any and all help will be appreciated.
Create a list or dictionary of Tkinter buttons to use in different frames
tkinter creating buttons in for loop passing command arguments
https://www.geeksforgeeks.org/looping-through-buttons-in-tkinter/#
Python: Assign commnds to buttons using a for loop when using Tkinter,
Here's my code
import customtkinter
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
bn = 0
schedule_list = ["On", "Off", "NA"]
button_dict = {}
# create scrollable frame
self.scrollable_frame = customtkinter.CTkScrollableFrame(self, width=750, height=385)
self.scrollable_frame.grid(row=0, column=0, sticky="nsew")
self.scrollable_frame.grid_columnconfigure(0, weight=1)
self.scrollable_frame_switches = []
# setup the schedule list
c = 0
l = 0
r = 0
for j in range(3):
bn += 1
# create the buttons
self.button = customtkinter.CTkButton(self.scrollable_frame, command=lambda bn=bn: self.button_event(bn))
self.button.grid(row=r, column=c, padx=20, pady=(10, 10))
self.button.configure(text=schedule_list[l])
self.scrollable_frame_switches.append(self.button)
l += 1
c += 1
def button_event(self,button_num):
self.button.configure(state="disabled")
print(button_num)
# if buttonNum == 1:
if __name__ == "__main__":
app = App()
app.mainloop()
*Update
I figured it out. Had to change the following code:
self.button = customtkinter.CTkButton(self.scrollable_frame, command=lambda bn=bn: self.button_event(bn))
to
self.button = customtkinter.CTkButton(self.scrollable_frame, command=lambda bn=bn: self.button_event(bn,self.scrollable_frame_switches))
Then change the button_event from
def button_event(self, button_num):
self.button_change.configure(state="disabled")
# if buttonNum == 1:
to
def button_event(self, button_num, switches):
button_change=self.scrollable_frame_switches[button_num]
button_change.configure(state="disabled")
self.scrollable_frame_switches.append(button_change)
Not an elegant way to do it, but it works. Thanks for the guidance all.