Tinter 定时上传按钮会卡死
打包成exe程序点击插入文件按钮会弹出一个多余的TK窗口界面,一关闭程序就崩溃
运行结果及详细报错内容
我想要达到可以根据选择的时间定时上传文件,不会有弹窗bug
"""代码如下:
#导入所有模块功能
import tkinter as tk
from tkinter import ttk, filedialog
import datetime
import time
import os
import shutil
import platform
import schedule
import tkinter.messagebox
from tkinter import font
###########################################第一窗口界面设置################################################
window=tk.Tk()
window.title('FT3文件管理助手')
# 定义主体窗体大小
window.geometry("700x500+300+100")
# 设置主体窗口背景颜色
window.config(background='cyan')
# 获取当前时间并设置主窗口时间显示格式
def update_time():
current_time = time.strftime('%H:%M:%S')
time_label.config(text=current_time)
time_label.after(1000, update_time)
def update_date():
current_date = datetime.date.today().strftime('%Y-%m-%d')
date_label.config(text=current_date)
# 设置主界面时间字体显示格式
time_font = font.Font(family="Helvetica", size=40, weight="bold")
date_font = font.Font(family="Helvetica", size=40)
# 设置主界面文本标签
time_label = tk.Label(window, text="", font=time_font, bg='white')
time_label.grid(row=15,column=70)
date_label = tk.Label(window, text="", font=date_font, bg='white')
date_label.grid(row=10,column=70)
# 执行自定义时间功能函数
update_time()
update_date()
# 创建用户名标签
lblname=tk.Label(window,text='账号',bg='orange',font=('微软雅黑', 18),width=3,height=1)
# 布局行列展示ipadx水平方向内边距,ipady外边距
lblname.grid(row=6,column=4,ipadx=10,ipady=10)
# 创建用户名文本输入框变量绑定
name1=tk.StringVar()
# 创建用户名文本输入框
entrypass=tk.Entry(window,textvariable=name1)
entrypass.grid(row=6,column=5,columnspan=2)
# 创建密码标签
lblname2=tk.Label(window,text='密码',font=('微软雅黑',18),bg='Lime',width=3,height=1)
lblname2.grid(row=7,column=4,padx=10, pady=10, ipadx=10, ipady=10)
# 创建密码字符串变量绑定
name2=tk.StringVar()
# 创建密码文本输入框
entrypa=tk.Entry(window,show='*',textvariable=name2)
entrypa.grid(row=7,column=5,columnspan=2)
###########################################第二窗口界面设置################################################
# 主界面定义提示框密码输入正确或错误的函数
def botton1_click():
# 设置输入密码成功后进入副界面,失败后提示密码错误!
if entrypass.get()=='123'and entrypa.get()=='1234':
# 副界面窗体
# 副界面进入主界面正常关闭
# window.destroy()
mainWin=tk.Tk()
# 副窗体界面大小设置
window.geometry("1050x600+200+100")
mainWin.config(background='cyan')
# 副窗体标题
mainWin.title('FT3文件管理界面')
# 创建副界面删除按钮
delete_button = tk.Button(mainWin, text="删除文件选择", command=lambda: delete_file())
delete_button.grid(row=7, column=0)
# 删除文件选择路径函数
def delete_file():
file_path = filedialog.askopenfilename()
delete_entry.insert(0, file_path)
# 删除按钮功能函数
def delete_file2():
file_path = delete_entry.get()
os.remove(file_path)
delete_entry.delete(0, tk.END)
# 副界面清理按钮
clean_button = tk.Button(mainWin, text="截图保存路径", command=lambda: clean_files())
clean_button.grid(row=8, column=0)
# 清理路径选择路径
def clean_files():
file_path = filedialog.askdirectory()
clean_entry.insert(0, file_path)
# 创建复制按钮
copy_button = tk.Button(mainWin, text="复制文件选择", command=lambda: copy_file())
copy_button.grid(row=9,column=0)
# 复制文件路径选择
def copy_file():
file_path = filedialog.askdirectory()
copy_entry.insert(0, file_path)
# 复制文件目标路径
def copy_file2():
file_path = filedialog.askdirectory()
copy2_entry.insert(0, file_path)
# 复制按钮功能将源文件路径文件复制到目标文件夹
def copy_click():
src_file = copy_entry.get()
dst_file = copy2_entry.get()
# 获取源文件下所有文件名,并赋值给变量files,并遍历该文件目录下所有文件复制到目标文件路径
files=os.listdir(src_file)
for file in files:
srcfile=os.path.join(src_file,file)
# 将源文件复制至目标路径path2中,目标文件夹如有相同文件名或内容则不会被覆盖
#shutil.copy(os.path.join(src_file,file),os.path.join(dst_file,file))
shutil.copytree(srcfile,os.path.join(dst_file,file))
# 创建文件移动按钮
move_button = tk.Button(mainWin, text="移动文件选择", command=lambda: move_file())
move_button.grid(row=10,column=0)
# 文件移动路径选择
def move_file():
file_path = filedialog.askdirectory()
move_entry.insert(0, file_path)
# 文件移动目标路径
def move_file2():
file_path = filedialog.askdirectory()
move_entry2.insert(0, file_path)
# 定义移动该路径下所有文件移动至目标文件路径功能函数,该路径从输入框中获得
def move_click():
# 定义源径位置赋值变量path1
path1= move_entry.get()
# 定义目标径位置赋值变量path2
path2= move_entry2.get()
# 获取源路径文件夹下面的所有文件名称
files=os.listdir(path1)
# 对文件夹下面文件进行遍历
for file in files:
# 遍历后合并拼接文件名
srcfile=os.path.join(path1,file)
# 将源文件移动至目标路径path2中,同名文件或内容会被覆盖,原文件夹内容转移
shutil.move(os.path.join(path1,file),os.path.join(path2,file))
# 运行成功后,窗口弹窗提示
tk.messagebox.showinfo('备份完成')
# 文件分类路径选择
def sortfile():
file_path = filedialog.askdirectory()
sort_entry.insert(0, file_path)
# 文件分类整理按钮功能函数
def sortfile2():
desktopPath = sort_entry.get()
allItems = os.listdir(desktopPath)
# 使用for循环遍历所有文件(夹)
for item in allItems:
# 获取文件后缀名
extension = os.path.splitext(item)[1].lower()
# 定义一个变量targetPath,用来表示准备移动到的文件夹路径
targetPath = ""
if extension in [".jpg", ".jpeg", ".gif", ".png", ".bmp"]:
# 使用os.path.join()函数拼接分类文件夹路径:图片文件
# 并赋值给变量targetPath
targetPath = os.path.join(desktopPath, "图片文件")
elif extension in [".avi", ".mp4", ".wmv", ".mov", ".flv"]:
# 使用os.path.join()函数拼接分类文件夹路径:视频文件
# 并赋值给变量targetPath
targetPath = os.path.join(desktopPath, "视频文件")
elif extension in [".wav", ".mp3", ".mid", ".ape", ".flac"]:
# 使用os.path.join()函数拼接分类文件夹路径:音频文件
targetPath = os.path.join(desktopPath, "音频文件")
elif extension in [".pdf"]:
# 使用os.path.join()函数拼接分类文件夹路径:PDF文件
targetPath = os.path.join(desktopPath, "PDF文件")
elif extension in [".docx", ".doc"]:
# 使用os.path.join()函数拼接分类文件夹路径:Word文件
targetPath = os.path.join(desktopPath, "Word文件")
elif extension in [".xlsx", ".xls"]:
# 使用os.path.join()函数拼接分类文件夹路径:Excel文件
targetPath = os.path.join(desktopPath, "Excel文件")
elif extension in [".pptx", ".ppt"]:
# 使用os.path.join()函数拼接分类文件夹路径:PPT文件
targetPath = os.path.join(desktopPath, "PPT文件")
else:
# 使用os.path.join()函数拼接分类文件夹路径:其他文件
targetPath = os.path.join(desktopPath, "其他文件")
# 判断当如果目标文件夹不存在
if not os.path.exists(targetPath):
# 使用os.mkdir()函数创建文件夹
os.mkdir(targetPath)
# 使用os.path.join()函数拼接desktopPath和文件名
# 并赋值给变量itemPath
itemPath = os.path.join(desktopPath, item)
# 判断当itemPath不是文件夹时。
if not os.path.isdir(itemPath):
# 使用shutil.move()函数移动文件到targetPath路径
shutil.move(itemPath, targetPath)
# 上传文件1源路径选择
def upload_file():
file_path = filedialog.askdirectory()
upload_entry.insert(0, file_path)
# 上传文件1目标路径选择
def upload_file2():
file_path = filedialog.askdirectory()
update_entry.insert(0, file_path)
# 上传文件2源路径选择
def upload_file3():
file_path = filedialog.askdirectory()
upload_entry2.insert(0, file_path)
# 中转站路径选择
def transfer_file():
file_path = filedialog.askdirectory()
transfer_entry.insert(0, file_path)
# 上传按钮触发功能
def upload_click():
# 定义文件源路径位置赋值变量path1
path1= upload_entry.get()
# 定义目标径位置赋值变量path2
path2= update_entry.get()
# 获取源路径文件夹下面的所有文件名称
files=os.listdir(path1)
# 对文件夹下面文件进行遍历
for file in files:
# 遍历后合并拼接文件名
srcfile=os.path.join(path1,file)
# 将源文件移动至目标路径path2中,同名文件或内容会被覆盖,原文件夹内容转移
shutil.move(os.path.join(path1,file),os.path.join(path2,file))
# 运行成功后,窗口弹窗提示
#tk.messagebox.showinfo('备份完成')
# 启动自动上传按钮功能
def buildfile():
# 文件自动创建函数
def create_dir(path):
# 判断文件是否存在,如果不存在则创建文件,存在则不创建文件
if not os.path.exists(path):
os.makedirs(path)
print(path + ' 上传目录1创建成功')
else:
print(path + ' 目录1已存在')
# 复制移动文件功能
def copy_files(src, dst):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
if not os.path.exists(d):
os.makedirs(d)
copy_files(s, d)
else:
shutil.copy2(s, d)
# 设置路径变量
if __name__ == '__main__':
# 上传1文本框获得的文本路径
src_path = upload_entry.get()
# 中转站文本框获得的文本路径
dst_path = transfer_entry.get()
# 根据时间转换成文件名字符串
date_str = time.strftime('%Y%m%d-%H%M', time.localtime())+str(platform.node())
# 拼接路径和以时间格式命名的文件夹
new_folder = os.path.join(dst_path, date_str)
# 创建文件夹
create_dir(new_folder)
# 复制文件
copy_files(src_path, new_folder)
src_path = upload_entry2.get()
if src_path is not None:
def create_dir(path):
# 判断文件是否存在,如果不存在则创建文件,存在则不创建文件
if not os.path.exists(path):
os.makedirs(path)
print(path + ' 上传目录2创建成功')
else:
print(path + ' 目录2已存在')
# 复制移动文件功能
def copy_files(src, dst):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
if not os.path.exists(d):
os.makedirs(d)
copy_files(s, d)
else:
shutil.copy2(s, d)
# 设置路径变量
if __name__ == '__main__':
# 上传1文本框获得的文本路径
src_path = upload_entry2.get()
# 中转站文本框获得的文本路径
dst_path = transfer_entry.get()
# 根据时间转换成文件名字符串
date_str = time.strftime('%Y%m%d-%H%M', time.localtime())+str(platform.node())
# 拼接路径和以时间格式命名的文件夹
new_folder = os.path.join(dst_path, date_str)
# 创建文件夹
create_dir(new_folder)
# 复制文件
copy_files(src_path, new_folder)
# 中转站需要移动文件路径
path1= transfer_entry.get()
# 定义目标径位置赋值变量path2
path2= update_entry.get()
# 获取源路径文件夹下面的所有文件名称
files=os.listdir(path1)
# 对文件夹下面文件进行遍历
for file in files:
# 遍历后合并拼接文件名
srcfile=os.path.join(path1,file)
# 将源文件移动至目标路径path2中,同名文件或内容会被覆盖,原文件夹内容转移
shutil.move(os.path.join(path1,file),os.path.join(path2,file))
print("文件已上传至目标文件夹")
# 创建每分钟定时计划任务
def buildfile1():
schedule.every(int(minute_spinbox.get())).minutes.do(buildfile)
while True:
schedule.run_pending()
time.sleep(2)
#show_entry.get(print("文件已上传至目标文件夹"))
# 创建每小时定时计划任务
def buildfile2():
schedule.every(int(hour_spinbox.get())).hours.do(buildfile)
while True:
schedule.run_pending()
time.sleep(2)
# 创建每天定时计划任务
def buildfile3():
schedule.every(int(day_combobox.get())).days.do(buildfile)
while True:
schedule.run_pending()
time.sleep(2)
# 创建每周定时计划任务
def buildfile4():
schedule.every(int(week_combobox.get())).weeks.do(buildfile)
while True:
schedule.run_pending()
time.sleep(2)
def buildfile5():
A=week_combobox.get()
B=hour_spinbox.get()
C=minute_spinbox.get()
D=f"{B}:{C}"
if "星期一" in A:
schedule.every().monday.at(D).do(buildfile)
elif "星期二" in A:
schedule.every().Tuesday.at(D).do(buildfile)
elif "星期三" in A:
schedule.every().Wednesday.at(D).do(buildfile)
elif "星期四" in A:
schedule.every().thursday.at(D).do(buildfile)
elif "星期五" in A:
schedule.every().friday.at(D).do(buildfile)
elif "星期六" in A:
schedule.every().saturday.at(D).do(buildfile)
elif "星期日" in A:
schedule.every().sunday.at(D).do(buildfile)
while True:
schedule.run_pending()
time.sleep(2)
# 副界面功能按钮
botton3=tk.Button(mainWin,text='文 件 整 理',command=sortfile2)
botton3.grid(row=11,column=2)
# 副界面功能按钮
botton4=tk.Button(mainWin,text='文件分类选择',command=sortfile)
botton4.grid(row=11,column=0)
# 创建上传文件按钮
upload_button = tk.Button(mainWin, text="上传选择 1", command=lambda: upload_file())
upload_button.grid(row=0,column=2)
upload_button2 = tk.Button(mainWin, text="上传选择 2", command=lambda: upload_file3())
upload_button2.grid(row=1,column=2)
# 中转站自动创建文件夹
transfer_botton=tk.Button(mainWin, text="中转站路径", command=lambda: transfer_file())
transfer_botton.grid(row=2,column=2)
# 创建默认目标路径选择
botton5=tk.Button(mainWin,text='复制至路径',command=copy_file2)
botton5.grid(row=9,column=2)
botton6=tk.Button(mainWin,text='移动至路径',command=move_file2)
botton6.grid(row=10,column=2)
botton7=tk.Button(mainWin,text='上传至路径',command=upload_file2)
botton7.grid(row=3,column=2)
# 创建删除按钮
botton8=tk.Button(mainWin,text='删 除 文 件',command=delete_file2)
botton8.grid(row=7,column=2)
# 创建清理重复文件按钮
botton9=tk.Button(mainWin,text='自 动 截 图',)
botton9.grid(row=8,column=2)
# 复制文件按钮
botton10=tk.Button(mainWin,text='复制',font=('微软雅黑',10),bg='SpringGreen',command=copy_click)
botton10.grid(row=9,column=5)
# 移动文件按钮
botton11=tk.Button(mainWin,text='移动',font=('微软雅黑',10),bg='SpringGreen',command=move_click)
botton11.grid(row=10,column=5)
# 创建上传文件按钮
# botton12=tk.Button(mainWin,text='上传',font=('微软雅黑',10),bg='SpringGreen',command=upload_click)
# botton12.grid(row=11,column=5)
# 创建按小时执行按钮
botton13 = tk.Button(mainWin, text="每()小时上传",command=buildfile2)
botton13.grid(row=20,column=2)
# 创建按分钟执行按钮
botton14=tk.Button(mainWin, text="每()分钟上传",command=buildfile1)
botton14.grid(row=19,column=2)
# 创建按天执行按钮
botton15=tk.Button(mainWin, text="每()天数上传",command=buildfile3)
botton15.grid(row=12,column=2)
# 创建按周执行按钮
# botton16=tk.Button(mainWin, text="每 ()几周上传",command=buildfile4)
# botton16.grid(row=17,column=2)
botton17=tk.Button(mainWin, text="备 份 定 时",bg='SpringGreen',command=buildfile5)
botton17.grid(row=4,column=2)
# 创建删除按钮路径文本框
delete_entry = tk.Entry(mainWin,width=55)
delete_entry.grid(row=7, column=1, padx=5, pady=5)
# 创建清理按钮路径文本框
clean_entry = tk.Entry(mainWin,width=55)
clean_entry.grid(row=8, column=1, padx=5, pady=5)
# 创建复制按钮默认路径和目标路径文本框
copy_entry = tk.Entry(mainWin,width=55)
copy_entry.grid(row=9, column=1, padx=5, pady=5)
copy2_entry=tk.Entry(mainWin,width=50)
copy2_entry.grid(row=9, column=3, padx=5, pady=5)
# 创建文件移动原路径和目标路径文本框
move_entry = tk.Entry(mainWin,width=55)
move_entry.grid(row=10, column=1, padx=5, pady=5)
move_entry2=tk.Entry(mainWin,width=50)
move_entry2.grid(row=10, column=3, padx=5, pady=5)
# 创建文件上传路径文本框
upload_entry = tk.Entry(mainWin,width=50)
upload_entry.grid(row=0, column=3, padx=5, pady=5)
upload_entry2 = tk.Entry(mainWin,width=50)
upload_entry2.grid(row=1, column=3, padx=5, pady=5)
update_entry=tk.Entry(mainWin,width=50)
update_entry.grid(row=3, column=3, padx=5, pady=5)
# 创建文件整理路径文本框
sort_entry=tk.Entry(mainWin,width=55)
sort_entry.grid(row=11,column=1, padx=5, pady=5)
# 创建中转站选择路径文本框
transfer_entry=tk.Entry(mainWin,width=50)
transfer_entry.grid(row=2,column=3, padx=5, pady=5)
#transfer_entry.grid(row=2,column=3, padx=5, pady=5, ipadx=5, ipady=5)
# 创建月份选择框
# month_label = ttk.Label(mainWin, text="选择月份:")
# month_label.grid(row=0, column=0, padx=5, pady=5)
# month_combobox = ttk.Combobox(mainWin, values=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"])
# month_combobox.current(0)
# month_combobox.grid(row=0, column=1, padx=5, pady=5)
# 创建星期选择框
week_label = ttk.Label(mainWin, text="选择星期:")
week_label.grid(row=0, column=0, padx=5, pady=5)
week_combobox = ttk.Combobox(mainWin, values=["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"])
week_combobox.current(0)
week_combobox.grid(row=0, column=1, padx=5, pady=5)
# 创建日期选择框
day_label = ttk.Label(mainWin, text="选择天数:")
day_label.grid(row=12, column=0, padx=5, pady=5)
day_combobox = ttk.Combobox(mainWin, values=[str(i) for i in range(1, 32)])
day_combobox.current(0)
day_combobox.grid(row=12, column=1, padx=5, pady=5)
# 创建小时选择框
hour_label = ttk.Label(mainWin, text="选择小时:")
hour_label.grid(row=1, column=0, padx=5, pady=5)
hour_spinbox = ttk.Combobox(mainWin, values=["01", "02", "03", "04", "05", "06", "07","08","09",10,11,12,13,14,15,16,17,18,19,20,21,22,23])
hour_spinbox.grid(row=1, column=1, padx=5, pady=5)
# 创建分钟选择框
minute_label = ttk.Label(mainWin, text="选择分钟:")
minute_label.grid(row=2, column=0, padx=5, pady=5)
minute_spinbox = tk.Spinbox(mainWin, from_=10, to=59)
minute_spinbox.grid(row=2, column=1, padx=5, pady=5)
window.withdraw()
# 密码错误时返回弹窗提示
else:
tkinter.messagebox.showinfo(title='信息提示', message='账号或密码错误!')
###################################################第一窗口界面按钮功能设置################################################
# 创建登录按钮
botton1=tk.Button(window,text='用户登录',font=('微软雅黑',18),bg='Seashell',command=botton1_click)
botton1.grid(row=10,column=5)
######################################################################################################
window.mainloop()
# 退出程序
#def window_quit():
# mainWin.quit()
# mainWin.destroy()
# exit()