使用的是flask web框架,本来想通过前端不断通过请求后端来实现进度条实时更新,无奈封装的ftp上传类回调中无法写业务代码逻辑,前端展示可参考https://blog.csdn.net/q1w2e3r4470/article/details/72702406
from tkinter import *
from tkinter import filedialog, ttk
import time
root = Tk() # 创建一个Tkinter.Tk()实例
# root.withdraw() #隐藏窗口
root.wm_attributes('-topmost', 1)
root.title('消息')
root.geometry('600x300+450+200')
localName = ''
def up():
up.place_forget()
select.place_forget()
mpb = ttk.Progressbar(root, orient="horizontal", length=200, mode="determinate")
mpb.pack()
mpb["maximum"] = 100
mpb["value"] = 0
Label(root, text='正在上传...', fg='red').pack()
for i in range(100):
mpb["value"] = i + 1
root.update()
time.sleep(0.1)
def callback():
global localName
localName = filedialog.askopenfilename(title=u"选择文件")
if localName == '':
lb.config(text="您没有选择任何文件")
else:
lb.config(text=localName)
select.config(text="重新上传")
select.place(anchor=CENTER, x=240, y=60)
up.config(text="确认上传", )
up.place(anchor=CENTER, x=330, y=60)
up.pack_forget()
lb = Label(root, text='')
lb.pack()
select = Button(root, text='选择文件', command=callback)
select.pack()
up = Button(root, text='', width=0, height=0, command=up)
up.pack_forget()
print(localName)
root.mainloop()
布局参考博客
https://blog.csdn.net/liuxu0703/article/details/54428405
xxx_forget 实现隐藏按钮
wm_attributes('-topmost', 1) 实现弹框靠前
geometry 弹框大小位置
update 更新进度条数据
*在button中的command 回调函数中是没有返回值的,如果需要可以使用 global,nonlocal修饰外部引用