python mainloop作用_(Python)我的脚本卡在root.mainloop(),如何让它与我的脚本一起运行?...

我的代码基本上做的是将.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值