简体   繁体   English

检查输入任务的时间是否已过

[英]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.我正在尝试制作一个待办事项列表,您可以在其中通过 TKinter 输入格式为“13:45 带走狗”的任务。 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.然后,我将任务中提到的时间处理为日期时间 object,我将其与当前时间进行比较以查看任务是否到期。 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.我想让 sound_alarm function 连续运行,以检查任务是否到期。 But i can't do that with a while true: loop as that interferes with the root.mainloop() from TKinter.但是我不能用 while true: loop 来做到这一点,因为它会干扰 TKinter 中的 root.mainloop()。

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?任何想法如何让它在您输入任务时,sound_alarm function 从该点开始运行以检查是否有任何任务到期?

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()

You can use .after() to execute sound_alarm() every minute.您可以使用.after() sound_alarm()每分钟执行一次 sound_alarm() 。

Below is modified sound_alarm() :下面是修改后的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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM