python - 在随机持续时间后停止检查对文件的更改
问题描述
我正在使用 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)
编辑:好的,我刚刚设法通过从网络驱动器运行它并故意断开连接来在家中重新创建它。我不明白的是,为什么它一旦重新连接就不会继续。
解决方案
推荐阅读
- android-management-api - 如果我们为移动设备管理(超过 1000 台设备)实施 Android 管理 API,Google 会收费吗?如果是这样,收费多少?
- python - 由于用逗号分隔字符串中的值而导致的错误
- mongodb - MongoDB 使用另一个集合中的另一个字段更新字段
- javascript - 如何使用 React 在 Accordion 中选择多个复选框?
- google-photos - Google Photo API 方法:albums.patch "isWriteable": true 不工作
- swiftui - SwiftUI NavigationLink 自行弹出
- elasticsearch - ElasticSearch 绑定服务失败
- amazon-web-services - PHP 会话在 AWS beanstalk 和 memcached 上不起作用
- mysql - 如何在 MySQL Workbench 中添加特定于列的现有表?
- python - 在 Windows 中安装 GNU gettext 的最佳解决方案是什么?