комбинируя ткинтер и сторож - PullRequest
1 голос
/ 11 марта 2019

Я пытаюсь следующее

  • Создайте графический интерфейс с tkinter, где пользователь выберет каталог для просмотра
  • Закрыть окно
  • Передайте путь к каталогу для сторожевого таймера, чтобы он мог отслеживать изменения файлов

Как можно объединить оба сценария в одном приложении?

В этом посте есть скрипт, который ничего не делает, когда я добавляю файл * .jpg в мою временную папку (osx). https://stackoverflow.com/a/41684432/11184726

Может ли кто-нибудь указать мне курс или учебное пособие, которые помогут мне понять, как сочетать происходящее.

1. GUI:

from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import filedialog

from tkinter.messagebox import showerror

globalPath = ""

class MyFrame(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("Example")
        self.master.rowconfigure(5, weight=1)
        self.master.columnconfigure(5, weight=1)
        self.grid(sticky=W+E+N+S)

        self.button = Button(self, text="Browse", command=self.load_file, width=10)
        self.button.grid(row=1, column=0, sticky=W)

    def load_file(self):

        fname = askopenfilename(filetypes=(("Text File", "*.txt"),("All files", "*.*") ))
        global globalPath
        globalPath = fname
#         fname = askopenfilename(filetypes=(("Text File", "*.txt"),("All files", "*.*") ))
        if fname:
            try:
                print("""here it comes: self.settings["template"].set(fname)""")
                print (fname)
            except:                     # <- naked except is a bad idea
                showerror("Open Source File", "Failed to read file\n'%s'" % fname)
            return

if __name__ == "__main__":

    MyFrame().mainloop() # All above code will run inside window
print(__name__)
print("the path to the file is : " + globalPath)

2. Сторожевой таймер:

from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

def on_created(event):
    # This function is called when a file is created
    print(f"hey, {event.src_path} has been created!")

def on_deleted(event):
    # This function is called when a file is deleted
    print(f"what the f**k! Someone deleted {event.src_path}!")

def on_modified(event):
    # This function is called when a file is modified
    print(f"hey buddy, {event.src_path} has been modified")
    #placeFile() #RUN THE FTP


def on_moved(event):
    # This function is called when a file is moved    
    print(f"ok ok ok, someone moved {event.src_path} to {event.dest_path}")




if __name__ == "__main__":



    # Create an event handler
    patterns = "*" #watch for only these file types "*" = any file
    ignore_patterns = ""
    ignore_directories = False
    case_sensitive = True

    # Define what to do when some change occurs 
    my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)
    my_event_handler.on_created = on_created
    my_event_handler.on_deleted = on_deleted
    my_event_handler.on_modified = on_modified
    my_event_handler.on_moved = on_moved

    # Create an observer
    path = "."
    go_recursively = False # Will NOT scan sub directories for changes 

    my_observer = Observer()
    my_observer.schedule(my_event_handler, path, recursive=go_recursively)

    # Start the observer
    my_observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        my_observer.stop()
    my_observer.join() 

1 Ответ

0 голосов
/ 15 марта 2019

Ниже приведен пример кода для объединения двух сценариев (не точно скопируйте два сценария в один, но показывая концепцию того, что вы запрашиваете):

from tkinter import *
from tkinter import filedialog
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

class Watchdog(PatternMatchingEventHandler, Observer):
    def __init__(self, path='.', patterns='*', logfunc=print):
        PatternMatchingEventHandler.__init__(self, patterns)
        Observer.__init__(self)
        self.schedule(self, path=path, recursive=False)
        self.log = logfunc

    def on_created(self, event):
        # This function is called when a file is created
        self.log(f"hey, {event.src_path} has been created!")

    def on_deleted(self, event):
        # This function is called when a file is deleted
        self.log(f"what the f**k! Someone deleted {event.src_path}!")

    def on_modified(self, event):
        # This function is called when a file is modified
        self.log(f"hey buddy, {event.src_path} has been modified")

    def on_moved(self, event):
        # This function is called when a file is moved    
        self.log(f"ok ok ok, someone moved {event.src_path} to {event.dest_path}")

class GUI:
    def __init__(self):
        self.watchdog = None
        self.watch_path = '.'
        self.root = Tk()
        self.messagebox = Text(width=80, height=10)
        self.messagebox.pack()
        frm = Frame(self.root)
        Button(frm, text='Browse', command=self.select_path).pack(side=LEFT)
        Button(frm, text='Start Watchdog', command=self.start_watchdog).pack(side=RIGHT)
        Button(frm, text='Stop Watchdog', command=self.stop_watchdog).pack(side=RIGHT)
        frm.pack(fill=X, expand=1)
        self.root.mainloop()

    def start_watchdog(self):
        if self.watchdog is None:
            self.watchdog = Watchdog(path=self.watch_path, logfunc=self.log)
            self.watchdog.start()
            self.log('Watchdog started')
        else:
            self.log('Watchdog already started')

    def stop_watchdog(self):
        if self.watchdog:
            self.watchdog.stop()
            self.watchdog = None
            self.log('Watchdog stopped')
        else:
            self.log('Watchdog is not running')

    def select_path(self):
        path = filedialog.askdirectory()
        if path:
            self.watch_path = path
            self.log(f'Selected path: {path}')

    def log(self, message):
        self.messagebox.insert(END, f'{message}\n')
        self.messagebox.see(END)

if __name__ == '__main__':
    GUI()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...