binds to edit records
This commit is contained in:
@@ -54,6 +54,7 @@ class Application:
|
||||
self.config = Config()
|
||||
|
||||
self.address_list = []
|
||||
self.current_record: int | None = None
|
||||
|
||||
# json vars
|
||||
self.json_file_name = "address_data.json"
|
||||
@@ -78,6 +79,7 @@ class Application:
|
||||
tk.Button(top_frame, text="Insert", command=self.export_csv, width=button_width).grid(row=1, column=0)
|
||||
tk.Button(top_frame, text="Delete", command=self.export_csv, width=button_width).grid(row=1, column=1)
|
||||
|
||||
self.aktiv = tk.StringVar()
|
||||
self.firma = tk.StringVar()
|
||||
self.name = tk.StringVar()
|
||||
self.strasse = tk.StringVar()
|
||||
@@ -85,18 +87,22 @@ class Application:
|
||||
|
||||
edit_frame = tk.Frame(self.root)
|
||||
edit_frame.pack(side=tk.TOP, fill=tk.X)
|
||||
tk.Label(edit_frame, text="Firma").grid(row=0, column=0)
|
||||
tk.Label(edit_frame, text="Name").grid(row=0, column=1)
|
||||
tk.Label(edit_frame, text="Aktiv").grid(row=0, column=0)
|
||||
tk.Label(edit_frame, text="Firma").grid(row=0, column=1)
|
||||
tk.Label(edit_frame, text="Name").grid(row=0, column=2)
|
||||
tk.Label(edit_frame, text="Strasse").grid(row=0, column=3)
|
||||
tk.Label(edit_frame, text="Plz/Ort").grid(row=0, column=4)
|
||||
edit_aktiv = tk.Checkbutton(edit_frame, variable=self.aktiv, onvalue="x", offvalue="")
|
||||
edit_aktiv.grid(row=1, column=0)
|
||||
edit_firma = tk.Entry(edit_frame, textvariable=self.firma)
|
||||
edit_firma.grid(row=1, column=0)
|
||||
edit_firma.grid(row=1, column=1)
|
||||
edit_name = tk.Entry(edit_frame, textvariable=self.name)
|
||||
edit_name.grid(row=1, column=1)
|
||||
edit_name.grid(row=1, column=2)
|
||||
edit_strasse = tk.Entry(edit_frame, textvariable=self.strasse)
|
||||
edit_strasse.grid(row=1, column=3)
|
||||
edit_plz_ort = tk.Entry(edit_frame, textvariable=self.plz_ort)
|
||||
edit_plz_ort.grid(row=1, column=4)
|
||||
tk.Button(edit_frame, text="Update", command=self.update_record).grid(row=1, column=5)
|
||||
|
||||
data_frame = tk.Frame(self.root, bg="teal")
|
||||
data_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
|
||||
@@ -109,10 +115,26 @@ class Application:
|
||||
self.table.heading('4', text="Plz/Ort")
|
||||
self.table.pack()
|
||||
|
||||
self.table.bind("<ButtonRelease-1>", self.select_record)
|
||||
|
||||
self.load_file()
|
||||
|
||||
self.root.mainloop()
|
||||
|
||||
def select_record(self, event):
|
||||
self.current_record = self.table.focus()
|
||||
values = self.table.item(self.current_record, "values")
|
||||
entry_var_list = [self.aktiv, self.firma, self.name, self.strasse, self.plz_ort]
|
||||
for i in range(len(values)):
|
||||
entry_var_list[i].set(values[i])
|
||||
|
||||
def update_record(self):
|
||||
if self.current_record is None:
|
||||
return
|
||||
values = (self.aktiv.get(), self.firma.get(), self.name.get(), self.strasse.get(), self.plz_ort.get())
|
||||
self.table.delete(self.current_record)
|
||||
self.table.insert('', self.current_record, values=values)
|
||||
|
||||
def load_file(self):
|
||||
try:
|
||||
with open(self.json_file, "r", encoding="utf-8") as f:
|
||||
@@ -175,7 +197,6 @@ class Application:
|
||||
except FileNotFoundError:
|
||||
self.show_error("Unexpected error", f"Could not write file {csv_file}")
|
||||
|
||||
|
||||
def populate_table(self):
|
||||
self.delete_all_table_items()
|
||||
for index, item in enumerate(self.address_list):
|
||||
|
||||
Reference in New Issue
Block a user