我正在尝试以下
- 使用 tkinter 创建一个 GUI,用户将在其中选择要观看的目录
- 关闭窗口
- 将目录的路径传递给看门狗,以便它可以监视文件更改
如何将两个脚本组合到一个应用程序中?
下面的帖子有一个脚本,当我将 *.jpg 文件添加到我的临时文件夹(osx)时,它什么都不做。 https://stackoverflow.com/a/41684432/11184726
有人可以向我指出一个课程或教程,这将帮助我了解如何结合正在发生的事情。
1.图形界面:
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()