Skip to content

Instantly share code, notes, and snippets.

@adamantonio
Created February 4, 2023 02:11
Show Gist options
  • Save adamantonio/6f11ae560c3a0af997305fe3f4a670cb to your computer and use it in GitHub Desktop.
Save adamantonio/6f11ae560c3a0af997305fe3f4a670cb to your computer and use it in GitHub Desktop.
Todo app with saving and loading tasks using pickle
import gooeypie as gp
import os
import pickle
def add_new_task(event):
"""Adds a new task from when the Enter/Return key is pressed"""
if event.key['name'] == 'Return' and new_task_inp.text != "":
todo_lst.add_item(new_task_inp.text)
new_task_inp.clear()
new_task_inp.focus()
def move_task(event):
"""Moves a task from one listbox to another"""
if event.widget == todo_lst:
# move the task from the todo list to the done list
if todo_lst.selected:
done_lst.add_item(todo_lst.remove_selected())
else:
# move the task back from the done list to the todo list
if done_lst.selected:
todo_lst.add_item(done_lst.remove_selected())
def delete_task(event):
"""Deletes a task from the todo list"""
todo_lst.remove_selected()
new_task_inp.focus()
def all_done(event):
"""Moves all tasks from the todo list to the done list"""
done_lst.items += todo_lst.items
todo_lst.items = []
def clear_all(event):
"""Remove all tasks from the done list"""
done_lst.items = []
def load_tasks():
"""Loads tasks from the tasks file and populates the todo and done Listboxes"""
try:
with open('tasks.pickle', 'rb') as tasks_file:
tasks = pickle.load(tasks_file)
todo_lst.items = tasks['todo']
done_lst.items = tasks['done']
except FileNotFoundError:
# If the tasks file is not there, do nothing
pass
except (KeyError, TypeError, pickle.UnpicklingError):
# If the file is not of the correct format, inform the user
message = 'The tasks file could not be loaded because it is not in the correct format'
app.alert('Could not load tasks file', message, 'info')
def save_tasks():
"""Saves the todo and done lists to the tasks file"""
try:
# Construct a dictionary to hold our two task lists
tasks = {
'todo': todo_lst.items,
'done': done_lst.items
}
# Save the tasks dictionary to a file in json format
with open('tasks.pickle', 'wb') as tasks_file:
pickle.dump(tasks, tasks_file)
except PermissionError:
# Inform the user of the error and seek confirmation to exit
message = f'Could not save tasks file in {os.getcwd()} due to a permission error.\n\nAre you sure you want to exit?'
return app.confirm_yesno('Cannot save tasks file', message, 'warning')
return True
app = gp.GooeyPieApp('Simple Todo')
app.width = 400
# Create all widgets
new_task_lbl = gp.Label(app, 'New task')
new_task_inp = gp.Input(app)
todo_lbl = gp.Label(app, 'Todo list')
todo_lst = gp.Listbox(app)
delete_task_btn = gp.Button(app, 'Delete task', delete_task)
all_done_btn = gp.Button(app, 'All done!', all_done)
done_lbl = gp.Label(app, 'Done!')
done_lst = gp.Listbox(app)
clear_all_btn = gp.Button(app, 'Clear all', clear_all)
# Set up events
new_task_inp.add_event_listener('key_press', add_new_task)
todo_lst.add_event_listener('double_click', move_task)
done_lst.add_event_listener('double_click', move_task)
# Add widgets to window
app.set_grid(5, 3)
app.set_column_weights(0, 1, 1)
app.set_row_weights(0, 1, 0, 1, 0)
app.add(new_task_lbl, 1, 1, align='right')
app.add(new_task_inp, 1, 2, column_span=2, fill=True)
app.add(todo_lbl, 2, 1, align='right')
app.add(todo_lst, 2, 2, column_span=2, fill=True, stretch=True)
app.add(delete_task_btn, 3, 2, fill=True)
app.add(all_done_btn, 3, 3, fill=True)
app.add(done_lbl, 4, 1, align='right')
app.add(done_lst, 4, 2, column_span=2, fill=True, stretch=True)
app.add(clear_all_btn, 5, 3, fill=True)
# Load any tasks and populate the listboxes
app.on_open(load_tasks)
# Save tasks when the app closes
app.on_close(save_tasks)
new_task_inp.focus()
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment