Search code examples
pythontkinterpython-datetime

Checking whether a time for an input task has passed


I'm trying to make a to do list where you can input tasks formatted as 'take out dog at 13:45' via TKinter. I then process the time mentioned in the task as a datetime object which i compare to the current time to see whether the task is due yet. However, i have ran into some trouble with the structure of it.

I want to have the sound_alarm function to run continuously, to check if the tasks are due yet. But i can't do that with a while true: loop as that interferes with the root.mainloop() from TKinter.

Any ideas how to have it so that when you enter tasks, the sound_alarm function starts running from that point to check whether any of the tasks is due yet?

Here is the code as i have it now:

import tkinter as tk
from tkinter import messagebox
import pickle
from datetime import datetime

root = tk.Tk()
root.title("To Do list")


def add_task():
    task = entry_task.get()
    if task != "":
        listbox_task.insert(tk.END, task)
        print(task)
        entry_task.delete(0, tk.END)
    else:
        tk.messagebox.showwarning(title="Warning", message="Enter a task first")


def del_task():
    try:
        task_index = listbox_task.curselection()[0]
        listbox_task.delete(task_index)
    except:
        tk.messagebox.showwarning(title="Warning", message="Select a task first")


def load_tasks():
    try:
        tasks = pickle.load(open("tasks.dat", "rb"))
        listbox_task.delete(0, tk.END)
        for task in tasks:
            listbox_task.insert(tk.END, task)
    except:
        tk.messagebox.showwarning(title="Warning", message="Cannot find task file")


def save_tasks():
    tasks = listbox_task.get(0, listbox_task.size())
    pickle.dump(tasks, open("tasks.dat", "wb"))


def get_time_from_task(task):
    time = task.split("at ", 1)[1]
    datetime_time = datetime.strptime(time, '%H:%M').time()
    return datetime_time


def sound_alarm():
    tasks = listbox_task.get(0, listbox_task.size())
    now = datetime.now().time()

    for task in tasks:
        datetime_time = get_time_from_task(task)
        print(datetime_time, now)
        if now > datetime_time:
            print(task, "is due!")


frame_tasks = tk.Frame(root)
frame_tasks.pack()

listbox_task = tk.Listbox(frame_tasks, height=20, width=50)
listbox_task.pack(side=tk.LEFT)

scrollbar_tasks = tk.Scrollbar(frame_tasks)
scrollbar_tasks.pack(side=tk.RIGHT, fill=tk.Y)

listbox_task.config(yscrollcommand=scrollbar_tasks.set)
scrollbar_tasks.config(command=listbox_task.yview)

entry_task = tk.Entry(root, width=50)
entry_task.pack()

btn_add = tk.Button(root, text="Add task", width=48, command=add_task)
btn_add.pack()

btn_del = tk.Button(root, text="Delete task", width=48, command=del_task)
btn_del.pack()

btn_load = tk.Button(root, text="Load tasks", width=48, command=load_tasks)
btn_load.pack()

btn_save = tk.Button(root, text="Save tasks", width=48, command=save_tasks)
btn_save.pack()

root.mainloop()

Solution

  • You can use .after() to execute sound_alarm() every minute.

    Below is modified sound_alarm():

    def sound_alarm():
        tasks = listbox_task.get(0, "end")
        now = datetime.now()
        current_time = now.time()
        for task in tasks:
            task_time = get_time_from_task(task)
            print(task_time, current_time)
            if current_time > task_time:
                print(task, "is due!")
    
        # try to schedule next check at 0 second of next minute
        delay = 60 - now.second
        root.after(delay*1000, sound_alarm)
    
    ...
    
    sound_alarm() # start the checking task
    root.mainloop()