我如何一起运行tkinter和看门狗 [英] how can i run both tkinter and watchdog together

查看:49
本文介绍了我如何一起运行tkinter和看门狗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,简而言之,我想做的就是让带有led和gui的arduino显示在不断更新的json文件(来自名为Elite Dangerous的游戏)中的信息.

So in short what im trying to do is to have an arduino with leds and a gui display the information stored within a continiously updating json file(its from a game called Elite Dangerous).

因此,我使用看门狗查看文件何时更新,然后读取新信息.然后,我想使用tkinter来显示信息,例如:改变颜色的盒子

So im using the watchdog to see when the file is updated and then read the new information. I would then like to use tkinter to display the information as for example: boxes changing color

但是问题是当我使用tkinter时,mainloop()阻止了其余代码的运行(假设这是因为它是一个无限循环).我对编程还是很陌生,所以请轻描淡写:P我对该网站也很陌生,所以请告诉我是否有任何信息

However the problem is that when i use tkinter the mainloop() stops the rest of the code from running (im assuming this is because its an infinite loop). Im very new to programming still so please be genlte :P Im quite new to this site as well so please tell me if any information is missing

def main(file_path=None):
    watched_dir = os.path.split(file_path)[0]
    print ('watched_dir = {watched_dir}'.format(watched_dir=watched_dir)) #prints the directory
    patterns = [file_path]
    print ('patterns = {patterns}'.format(patterns=','.join(patterns))) #prints the file with path
    event_handler = MyEventHandler(patterns=patterns)

#creating the observer and watching the file
    observer = Observer()
    observer.schedule(event_handler, watched_dir, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
        observer.join()

推荐答案

下面是一起运行 tkinter watchdog 的示例:

Below is an example on running tkinter and watchdog together:

import tkinter as tk
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyEventHandler(FileSystemEventHandler):
    def __init__(self, callback):
        super().__init__()
        self.callback = callback

    def on_modified(self, event):
        self.callback(event.src_path)

def on_modified(target):
    txtbox.insert('end', target+' is modified\n')

event_handler = MyEventHandler(on_modified)
observer = Observer()
observer.schedule(event_handler, '.', recursive=False)

root = tk.Tk()

txtbox = tk.Text(root, width=40, height=20)
txtbox.pack()

observer.start()
root.mainloop()

observer.stop()
observer.join()

这篇关于我如何一起运行tkinter和看门狗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆