From f685ada2a571088d920a57d5e2ee7a015962f812 Mon Sep 17 00:00:00 2001 From: Ione 15 Date: Thu, 28 Mar 2024 14:39:47 +0100 Subject: [PATCH] cleaned up. fixed unable to delete index 0 --- main.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 194262c..1320b94 100644 --- a/main.py +++ b/main.py @@ -29,6 +29,7 @@ def add_item_dialog(): # Create an entry for adding items entry = tk.Entry(input_field, width=50) entry.pack(padx=10, pady=5) + entry.focus() input_confirm = tk.Button(input_field, text="Confirm", command=lambda: add_item(entry, input_field)) input_confirm.pack(side=tk.BOTTOM, padx=5, pady=5) @@ -41,21 +42,20 @@ def add_item(entry, input_field): if item: listbox.insert(tk.END, item) cur.execute("INSERT INTO todo(\"ID\",\"TODO\",\"CREATION_DATE\",\"DUE_DATE\",\"PRIO\",\"IMP\") VALUES (NULL, " - "'%s', '%s','%s', '%s', '%s' )" % (item, get_time(), get_time(), 100, 0)) + "'%s', '%s','%s', '%s', '%s' )" % (item, get_time(), get_time() + 600, 100, 0)) con.commit() - input_field.destroy() update_list() + input_field.destroy() def remove_item(): - selected_index = listbox.curselection() - if selected_index: - listbox.delete(selected_index) - print(selected_index[0]) - cur.execute("DELETE FROM todo WHERE ID = %s" % list(tasklist.keys())[selected_index[0]]) - print("DELETE FROM todo WHERE ID = %s" % list(tasklist.keys())[selected_index[0]]) - con.commit() - update_list() + selected_index = listbox.curselection()[0] # selection returns tupel + selected_task_id = list(tasklist.keys())[selected_index] + listbox.delete(selected_index) + cur.execute("DELETE FROM todo WHERE ID = %s" % selected_task_id) + print("DELETE FROM todo WHERE ID = %s" % selected_task_id) + con.commit() + update_list() def update_list(): @@ -68,21 +68,24 @@ def update_list(): tasklist.clear() res = cur.execute("SELECT * FROM todo").fetchall() - for i, tmp1 in enumerate(res): + for i, _tmp1 in enumerate(res): total_time.append(res[i][3] - res[i][2]) remaining_time.append(res[i][3] - cur_time) importance.append(total_time[i] / remaining_time[i] * res[i][4]) if int(importance[i]) == 0: importance[i] = 1 - print("Priority of Task ID " + str(i + 1) + " Set to: " + str(importance[i])) # res [i][0] = ID + print("Priority of Task ID " + str(res[i][0]) + " Set to: " + str(importance[i])) # res [i][0] = ID cur.execute("UPDATE todo SET IMP = %s WHERE ID = %s" % (int(importance[i]), res[i][0])) - tasklist[res[i][0]] = res[i][1] # create dictionary with id and the task con.commit() + res = cur.execute("SELECT * FROM todo ORDER BY IMP DESC").fetchall() + for i, _tmp2 in enumerate(res): + tasklist[res[i][0]] = res[i][1] # create dictionary with id and the task redraw_list() def redraw_list(): listbox.delete(0, listbox.size()) # clear listbox + print(tasklist) for task in tasklist.items(): listbox.insert(tk.END, task[1]) # [0] = id @@ -118,8 +121,10 @@ add_button.pack(side=tk.LEFT, padx=5, pady=5) remove_button = tk.Button(root, text="Remove", command=remove_item) remove_button.pack(side=tk.LEFT, padx=5, pady=5) -update_button = tk.Button(root, text="Update", command=update_list) +update_button = tk.Button(root, text="Reload", command=update_list) update_button.pack(side=tk.LEFT, padx=5, pady=5) +update_list() + # Run the Tkinter event loop root.mainloop()