【问题标题】:Multiple timers at once多个计时器一次
【发布时间】:2019-03-02 12:42:15
【问题描述】:

编辑:我现在得到一个运行时错误:当第二个计时器达到 0 并调用 Note() 函数时,'主线程不在主循环中'

from datetime import datetime
from threading import Timer
from tkinter import messagebox
import json


def Note():
    z = messagebox.showinfo("Note", note)

class Timers:
    def __init__(self,a,b,c,d,e):
        self.a = month
        self.b = day
        self.c = hour
        self.d = minute
        self.e = note
        x = datetime.today()
        y = x.replace(month=month, day=day, hour=hour, minute=minute, second=0, microsecond=0)
        delta_t = y - x
        secs = delta_t.seconds


        T = Timer(secs, Note)
        T.start()

【问题讨论】:

    标签: python python-3.x variables import timer


    【解决方案1】:

    您可能想使用 json 来存储数据对象列表

    def save_some_object(a,b,c):
        with open("some_file.txt","wb") as f:
             json.dump({'a':a,'b':b},f)
    

    在您的情况下,您要保存的对象是日期时间元数据的 LIST

    alarms_list = [{'hour':5,'minute':5,'second':0,'day':1,'month':1,'year':2019}, {'hour':6,'minute':5,'second':0,'day':1,'month':1,'year':2021}]

    然后您可以将它们保存到您的文件中

    with open("some_file.txt","wb") as f:
         json.dump(alarms_list,f)
    

    您也可以使用 json 加载警报列表

    with open("some_file.txt","rb") as f:
         my_alarm_data_list = json.load(f)
    

    【讨论】:

    • 谢谢,我已经走得更远了,但仍有问题。当第二个计时器变为 0 时,我收到错误消息:“主线程不在主循环中”。我用我的新代码更新了我的问题,所以也许你可以帮助我更多。提前致谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多