python - 如何修复我的 python kivy 文件夹监控应用程序崩溃
问题描述
我刚刚开始使用 python kivy 开发一个 gui 应用程序,它可以让我的计算机上的监视器文件夹,现在我有 main.py、main.kv 和 watchdog1.py 文件,当我运行我的代码时,应用程序运行和输出文件夹活动到我的终端,但是由于内存使用过多,它在大约一分钟后崩溃。
这是我的 main.py:
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.label import Label
from kivy.uix.checkbox import CheckBox
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.properties import StringProperty, NumericProperty, ObjectProperty
from kivy.graphics import Color, Rectangle, Ellipse, Line
from watchdog1 import watch
import _thread
WIDTH, HEIGHT = 1000, 800
class MainWindow(AnchorLayout):
bbox = ObjectProperty(None)
folders = ["Desktop", "Documents", "Downloads"]
labels = []
bools = {}
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(self.add_initial, .1)
Clock.schedule_interval(self.check_active, .1)
def add_initial(self, dt):
for f in self.folders:
self.add_folder(f)
def add_folder(self, text):
f = Label(text=text)
s = CheckBox()
b = BoxLayout()
b.add_widget(f)
b.add_widget(s)
self.bbox.add_widget(b)
self.labels.append(text)
self.bools[text] = 0
def check_active(self, dt):
for widget in self.bbox.children:
t = widget.children[1].text
b = widget.children[0].active
if b is True and self.bools[t] == 0:
print(t, "is active")
watch(f"C:/Users/Chris/{t}")
self.bools[t] = 1
elif b is False:
self.bools[t] = 0
class MainApp(App):
def build(self):
Window.size = (WIDTH, HEIGHT)
self.title = "Kivy Watchdog"
return
if __name__ == "__main__":
MainApp().run()
和我的 main.kv
MainWindow:
<MainWindow>:
bbox: bbox
BoxLayout:
orientation: "vertical"
BoxLayout:
size_hint: 1, None
height: dp(30)
Label:
text: "Watchdog"
BoxLayout:
Label:
text: "Folders\nto\nMonitor"
size_hint: .1, 1
BoxLayout:
canvas:
Color:
rgba: .2, .2, .2, 1
Rectangle:
size: self.size
pos: self.pos
orientation: "vertical"
spacing: 10
id: bbox
和我的 watchodg1.py
import multiprocessing
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
def watch(p):
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = p
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()
解决方案
推荐阅读
- linux - 如何编写脚本对多个 csv 文件执行一些基本操作
- python - 抓取多个表并将一些行作为列标题
- mysql - 如何创建触发器以从另一个表中插入分组数据
- php - 如何将带有索引的动态输入中的数据插入到数据库中
- r - 在 R 中的工作表列表中选择特定列 - 循环
- vue.js - 如何重定向和获取 url 参数
- elasticsearch - 为什么我的 grok 过滤器不解析我的 filebeats 消息?我看不到 Kibana (ELK) 中的 logstash 解析字段
- javascript - 不能 .map() 似乎是 Array 的值
- excel - 是否有一段代码可以从 Google 搜索中提取地址
- blazor - 动态呈现 Blazor 组件的推荐方法是什么?