我正在制作一个Tkinter应用程序,它将文件从用户的系统复制到FTP服务器。
我想包括一个确定的进度条,它将显示通过服务器复制的文件的%进度。但是进度条不起作用,因为复制时窗口挂起。我在进度条中使用了线程和不确定模式,但它没有任何用处。请检查以下代码并建议适当的添加/修改。在from Tkinter import *
from ftplib import FTP
import ttk,threading,os,time,tkMessageBox
main_window=Tk()
def onCopy_buttonClick():
def ok_button_click():
i=0
global list1
for item in files:
if(list1[i].get()==1):
Ftp=FTP_Entry.get()
Ftp=Ftp.split("/",1)
site=FTP(Ftp[0])
site.login(User_Entry.get(),Password_Entry.get())
site.cwd(Ftp[1])
copy_window.withdraw()
progress_window=Toplevel(copy_window)
p_label=Label(progress_window,text="Please wait while the files are being copied..").pack()
progressbar=ttk.Progressbar(progress_window,length=300,orient='horizontal',mode='determinate')
progress=StringVar()
progresslabel=Label(textvariable=progress).pack()
progress.set("0 KBps")
progressbar.pack()
result=True
for file in site.nlst():
if item==file:
result=tkMessageBox.askyesno("Warning !",item+" is already present. If you select Yes, item will be replaced.")
if result==True:
def foo():
with open(item,"rb") as f:
site.storbinary("STOR "+item,f,blocksize=33554432)
def start_foo_thread():
foo_thread = threading.Thread(target=foo)
foo_thread.start()
size=int(os.path.getsize(item)) #item can be in GB's
copied=0
'''while(copied!=size):
copied=int(site.size(item)) #doesn't get size till copying is done
progressbar['value']=round(copied/size)
progress.set(" KBps") #how?
time.sleep(1000)'''
while(True):
if foo_thread.is_alive()==False:
progressbar.stop()
break
start_foo_thread()
i=i+1
progress_window.destroy()
copy_window.destroy()
main_window.deiconify()
os.chdir(Path_Entry.get())
copy_window=Toplevel(main_window)
main_window.withdraw()
files=os.listdir(os.getcwd())
global list1
list1=[]
for i in range(0,len(files)):
list1.append("0")
list1[i]=IntVar()
i=0
for item in files:
if os.path.isfile(item):
c = Checkbutton(copy_window,variable=list1[i],text=item)
c.pack()
i=i+1
ok_button=Button(copy_window,text="OK",command=ok_button_click).pack()
Path_Label=Label(text="Enter File path : ").grid(row=0,column=0)
Path_Entry=Entry()
Path_Entry.grid(row=0,column=1)
FTP_Label=Label(text="FTP Path : ").grid(row=1,column=0)
FTP_Entry=Entry()
FTP_Entry.grid(row=1,column=1)
User_Label=Label(text="Username : ").grid(row=2,column=0)
User_Entry=Entry()
User_Entry.grid(row=2,column=1)
Password_Label=Label(text="Password : ").grid(row=3,column=0)
Password_Entry=Entry()
Password_Entry.grid(row=3,column=1)
Copy_button=Button(text="START COPYING",command=onCopy_buttonClick)
Copy_button.grid(row=4,columnspan=2)
main_window.mainloop()
我现在已经编辑了代码,这就是它的外观,它符合我的目的。欢迎提出任何建议。在
^{pr2}$