我的代码基本上是将.txt文件从source_path复制到dest_path,你可以在没有所有tkinter代码的情况下测试它,看它是否有效。 现在,我想为我创建的这个小脚本创建一个GUI,并查看他们告诉我的教程,我必须写下root.mainloop()来创建一个保留的窗口。
我想要做的是(现在)让这个小窗口与我正在运行的脚本一起使用,但无论我把它放在哪里它都会停止我运行的代码并永远停留在root.mainloop()。
我看到有些人使用root.after(),但它似乎对我不起作用。我该如何解决这个问题?
import time, shutil, os, datetime
from tkinter import *
root = Tk() # defines tkinter
source_path = r"C:\SOURCE" # Your Source path
dest_path = r"C:\DESTINATION" # Destination path
file_ending = '.txt' # Needed File ending
files = os.listdir(source_path) # lits directorys in source_path
date = datetime.datetime.now().strftime('%d.%m.%Y') # get the current date
sleepingtime = 10 # amount of time sleeping
label = Label(root, text="some text")
while True:
label.pack()
root.mainloop()
print("Beginning checkup")
print("=================")
if not os.path.exists(source_path) or not os.path.exists(dest_path): # checks if directory exists
print("Destination/Source Path does not exist!")
else:
print("Destination exists, checking files...")
for f in files:
if f.endswith(file_ending):
new_path = os.path.join(dest_path, date + " - DO-PC")
src_path = os.path.join(source_path, f)
if not os.path.exists(new_path): # create the folders if they dont already exists
os.makedirs(new_path)
if not os.path.exists(os.path.join(new_path, f)):
print("copying " + src_path)
shutil.copy(src_path, new_path)
else:
print(src_path + " already copied")
print("=================")
print('Checkup done, waiting for next round...')
time.sleep(sleepingtime) # wait a few seconds between looking at the directory