I have a program that adds entry widgets to a frame along with a button. When I click the button I want to edit further details about the fields that button references. The button always references the last set of fields populated though, which makes sense as I can’t assign a unique name to the widget. How do I get the button to link to the set of fields?
class ToplevelWindow(customtkinter.CTkToplevel):
def __init__(self, uid, *args, **kwargs):
super().__init__(*args, **kwargs)
self.geometry("400x300")
self.uid = uid
self.label = customtkinter.CTkLabel(self, text=self.uid)
self.label.pack(padx=20, pady=20)
class ItemFrame(customtkinter.CTkFrame):
def __init__(self, master, door_list, **kwargs):
super().__init__(master, **kwargs)
# name / reader
self.door_list = door_list
for door in self.door_list:
print(f'DOOR POS IS {self.door_list.index(door)}')
self.uid = customtkinter.StringVar()
self.uid.trace_add('write', self.callback)
self.door_label = customtkinter.CTkLabel(self, text="Door Name")
self.door_label.grid(column=0, row=self.door_list.index(door)*3, padx=10, pady=(50, 2.5))
self.door_name = customtkinter.CTkEntry(self, placeholder_text=door, textvariable=self.uid)
self.door_name.grid(column=1, row=self.door_list.index(door)*3, pady=(50, 2.5))
self.in_label = customtkinter.CTkLabel(self, text="In Reader")
self.in_label.grid(column=2, row=self.door_list.index(door)*3, padx=10, pady=(50, 2.5))
self.in_reader = customtkinter.CTkEntry(self, placeholder_text=door)
self.in_reader.grid(column=3, row=self.door_list.index(door)*3, pady=(50, 2.5))
self.out_label = customtkinter.CTkLabel(self, text="Out Reader")
self.out_label.grid(column=4, row=self.door_list.index(door)*3, padx=10, pady=(50, 2.5))
self.out_reader = customtkinter.CTkEntry(self, placeholder_text=door)
self.out_reader.grid(column=5, row=self.door_list.index(door)*3, pady=(50, 2.5))
# zone /controller
self.con_label = customtkinter.CTkLabel(self, text="Controller")
self.con_label.grid(column=0, row=self.door_list.index(door)*3+1, padx=10, pady=2.5)
self.con_name = customtkinter.CTkEntry(self, placeholder_text=door)
self.con_name.grid(column=1, row=self.door_list.index(door)*3+1)
self.az_label = customtkinter.CTkLabel(self, text="Access Zone")
self.az_label.grid(column=2, row=self.door_list.index(door)*3+1, padx=10)
self.az = customtkinter.CTkEntry(self, placeholder_text=door)
self.az.grid(column=3, row=self.door_list.index(door)*3+1)
self.alz_label = customtkinter.CTkLabel(self, text="Alarm Zone")
self.alz_label.grid(column=4, row=self.door_list.index(door)*3+1, padx=10)
self.alz = customtkinter.CTkEntry(self, placeholder_text=door)
self.alz.grid(column=5, row=self.door_list.index(door)*3+1)
# I/O
self.dps_label = customtkinter.CTkLabel(self, text="DPS Name")
self.dps_label.grid(column=0, row=self.door_list.index(door)*3+2, padx=10, pady=2.5)
self.dpsname = customtkinter.CTkEntry(self, placeholder_text="DPS ")
self.dpsname.grid(column=1, row=self.door_list.index(door)*3+2)
self.rex_label = customtkinter.CTkLabel(self, text="REX Name")
self.rex_label.grid(column=2, row=self.door_list.index(door)*3+2, padx=10)
self.rexname = customtkinter.CTkEntry(self, placeholder_text="REX ")
self.rexname.grid(column=3, row=self.door_list.index(door)*3+2)
self.lock_label = customtkinter.CTkLabel(self, text="Lock Name")
self.lock_label.grid(column=4, row=self.door_list.index(door)*3+2, padx=10)
self.lockname = customtkinter.CTkEntry(self, placeholder_text="Lock ")
self.lockname.grid(column=5, row=self.door_list.index(door)*3+2)
self.save = customtkinter.CTkButton(self, text="Configure I/O", command=lambda: self.open_toplevel(self.uid.get()))
self.save.grid(column=6, row=self.door_list.index(door)*3+2, padx=5)
self.toplevel_window = None
def callback(self, var, indx, mode):
uid = self.uid.get()
print(uid)
def open_toplevel(self, uid):
if self.toplevel_window is None or not self.toplevel_window.winfo_exists():
self.toplevel_window = ToplevelWindow(uid=self.uid.get())
self.toplevel_window.attributes("-topmost", True)
else:
self.toplevel_window.focus()
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.title("Gallagher Bulk Import GUI")
self.geometry("1050x650")
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=0)
self.door_list = []
self.itemframe = ItemFrame(self, door_list=self.door_list)
self.itemframe.grid(row=0, column=0)
self.button = customtkinter.CTkButton(self, text="New Door", command=self.add_door)
self.button.grid(row=len(itemlist)+25, column=0, padx=10, pady=10, sticky="ew")
def add_door(self):
self.door_list = self.itemframe.door_list
self.itemframe.destroy()
numbox = len(self.door_list) + 1
self.door_list.append("NewDoor" + str(numbox))
self.itemframe = ItemFrame(self, door_list=self.door_list)
self.itemframe.grid(row=0, column=0)
print(self.door_list)