I want to create tkinter buttons in bulk by using a for loop and the exec function but when i set the command it keeps on calling the function for the last piece in my database
for i in FirstFloor:
exec('room_%d = CreateRoom(FirstFloor[i]["name"])'%index)
exec('lbl_%d = Button(window, text=FirstFloor[i]["name"], command=lambda: move(FirstFloor[i]["x"], FirstFloor[i]["y"]), bg="light grey")'%index)
exec('lbl_%d.grid(column=FirstFloor[i]["x"], row=FirstFloor[i]["y"], columnspan=FirstFloor[i]["xspan"], rowspan=FirstFloor[i]["yspan"])'%index)
if FirstFloor[i]["locked"] == True:
exec('lbl_%d.config(state="disabled", bg="red")'%index)
index += 1
When i run this piece of code and click a button no matter which button i press it keeps going to same object
exec. It makes the code harder to understand and harder to debug. Just use a dictionary or list to store all of your button references.exec()unless you know exactly how it works and how to use it I would avoid. It is 100% possible to do this withoutexec().