首页 > 解决方案 > 如何修复我的 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()

这是发生的情况的屏幕截图: 下圈显示记录工作,大圈显示错误

标签: pythonpython-3.xkivykivy-language

解决方案


推荐阅读