From fd0a78215d6c179d6e46c0543307721046c249a6 Mon Sep 17 00:00:00 2001 From: Ione 15 Date: Thu, 28 Mar 2024 20:18:59 +0100 Subject: [PATCH] Added Priorities, Auto List refresh --- fresh_db.sh | 2 +- main.py | 61 ++++++++++++++++++++++++++++++++------------------- txt_to_cmd.sh | 2 +- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/fresh_db.sh b/fresh_db.sh index 9421208..9d42bf5 100755 --- a/fresh_db.sh +++ b/fresh_db.sh @@ -23,7 +23,7 @@ done < "$file" for ((i=1; i> commands.sql + echo "INSERT INTO \"main\".\"todo\"(\"ID\",\"TODO\",\"CREATION_DATE\",\"DUE_DATE\",\"PRIO\",\"IMP\") VALUES (NULL,'${!var}','$date','$(($date+60))',1,0);" >> commands.sql done echo converted $file to sql statements sqlite3 todo.db ".read commands.sql" diff --git a/main.py b/main.py index f88e8cc..33188b0 100644 --- a/main.py +++ b/main.py @@ -31,34 +31,44 @@ def add_item_dialog(): entry.pack(padx=10, pady=5) entry.focus() - input_confirm = tk.Button(input_field, text="Confirm", command=lambda: add_item(entry, input_field)) + priority_selection_frame = tk.Frame(input_field) # container for prio and scale + priority_selection_frame.pack() + priority_label = tk.Label(priority_selection_frame, text="Priority:") + priority_label.pack(side=tk.LEFT, padx=25, pady=25) + priority_selection = tk.Scale(priority_selection_frame, from_=1, to=3, orient=tk.HORIZONTAL, width=20) + priority_selection.pack() + + input_confirm = tk.Button(input_field, text="Confirm", + command=lambda: add_item(entry, priority_selection, input_field)) input_confirm.pack(side=tk.BOTTOM, padx=5, pady=5) input_field.mainloop() -def add_item(entry, input_field): +def add_item(entry, priority_selection, input_field): item = entry.get() - print("added " + item) + priority = priority_selection.get() 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() + 600, 100, 0)) + "'%s', '%s','%s', '%s', '%s' )" % (item, get_time(), get_time() + 60, priority, 0)) con.commit() update_list() input_field.destroy() def remove_item(): - 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() + if listbox.curselection(): + 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(): + selected_task = listbox.curselection() # remember selection before rebuilding listbox.delete(0, listbox.size()) # clear listbox cur_time = get_time() @@ -71,23 +81,21 @@ def update_list(): 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])/total_time[i] * res[i][4]) - if int(importance[i]) == 0: - importance[i] = 1 - print("Priority of Task ID " + str(res[i][0]) + " Set to: " + str(importance[i])) # res [i][0] = ID + importance.append((total_time[i] - remaining_time[i]) / total_time[i] * res[i][4] * 100) cur.execute("UPDATE todo SET IMP = %s WHERE ID = %s" % (int(importance[i]), res[i][0])) 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() + redraw_list(selected_task) -def redraw_list(): +def redraw_list(selected_task): listbox.delete(0, listbox.size()) # clear listbox - print(tasklist) for task in tasklist.items(): listbox.insert(tk.END, task[1]) # [0] = id + if selected_task: + listbox.selection_set(selected_task) # restore selection before refresh, if existing # Create the main window @@ -112,19 +120,28 @@ root.minsize(window_width, window_height) # prevent making it smaller than min # Create a listbox to display the items listbox = tk.Listbox(root, width=100) -listbox.pack(padx=5, pady=5) +listbox.pack(padx=5, pady=5, expand=True, fill=tk.BOTH) # Create buttons to add and remove items add_button = tk.Button(root, text="Add", command=add_item_dialog) 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="Reload", command=update_list) update_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_list() # Run the Tkinter event loop +refresh_time = 15 + + +def refresh_ever_x_seconds(): + update_list() + current_time = root.after(1000 * refresh_time, refresh_ever_x_seconds) + + +refresh_ever_x_seconds() root.mainloop() diff --git a/txt_to_cmd.sh b/txt_to_cmd.sh index 2714da2..f40adc4 100755 --- a/txt_to_cmd.sh +++ b/txt_to_cmd.sh @@ -20,6 +20,6 @@ done < "$file" for ((i=1; i> commands.sql + echo "INSERT INTO \"main\".\"todo\"(\"ID\",\"TODO\",\"CREATION_DATE\",\"DUE_DATE\",\"PRIO\",\"IMP\") VALUES (NULL,'${!var}','$date','$(($date+60))',1,0);" >> commands.sql done echo converted $file to sql statements