首页 > 解决方案 > 在随机持续时间后停止检查对文件的更改

问题描述

我正在使用 Tkinter 为我工作的医院开发数字标牌。它通过读取保存到共享网络驱动器并从另一台计算机编辑以反映当前延迟的文本文件的内容来更新患者当前延迟的情况。

今天试用后,反馈是在看似随机的持续时间后,它会停止更新延迟以反映文本文件中保存的内容。解决它的唯一方法是关闭并重新打开。我正在努力在自己的计算机上重现该问题,因此我开始怀疑它可能是由于暂时失去网络连接造成的。

我假设它是导致问题的以下功能。如果有帮助,我也很乐意发布其余代码。

def check_delay(self):
    for name in self.label_id_list:
        index = self.label_id_list.index(name)
        location = self.label_name_list[index]
        path = os.path.join("config", f"{location}.txt")
        try:
            with open(path, "rt") as in_file:
                contents = in_file.read()
                if not contents or contents == "0":
                    name.config(text=f"{location.title()} is on time", foreground="green",
                                font=("", fontSize, "bold"))
                elif contents.isdigit():
                    name.config(text=f"{location.title()} is {contents} minutes delayed", foreground="red",
                                font=("", fontSize, "bold"))
                elif 'service' in contents:
                    name.config(text=f"{location.title()} is being serviced", foreground="red",
                                font=("", fontSize, "bold"))
                else:
                    name.config(text=f"null", foreground="black",
                                font=("", fontSize, "bold"))
        except FileNotFoundError:
            open(f"config/{location}.txt", 'w')
    self.after(1000, self.check_delay)

编辑:好的,我刚刚设法通过从网络驱动器运行它并故意断开连接来在家中重新创建它。我不明白的是,为什么它一旦重新连接就不会继续。

标签: pythonpython-3.xtkinter

解决方案


推荐阅读