繁体   English   中英

Tkinter进度栏不起作用

[英]Tkinter Progress Bar not working

我正在制作一个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()

我现在已经编辑了代码,这就是它的外观,它可以满足我的目的。 欢迎任何建议。

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')              
                progressbar.pack()
                progress=StringVar()
                progresslabel=Label(progress_window,textvariable=progress)
                progresslabel.pack()
                progress.set("0 % Copied")

                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 callback(block):
                        global sizewritten
                        sizewritten += 8388608                      
                    def foo():
                        with open(item,"rb") as f:
                            site.storbinary("STOR "+item,f,8388608,callback)    
                    def start_foo_thread():
                        filesize=int(os.path.getsize(item))
                        progressbar["maximum"] = filesize
                        foo_thread = threading.Thread(target=foo)
                        foo_thread.start()                      
                        while(True):
                            progressbar["value"]=sizewritten
                            percent=str(int(sizewritten/float(filesize)*100))
                            if (int(percent)>100):
                                percent="100"
                            progress.set(percent+" % Copied")   
                            progress_window.update()
                            if foo_thread.is_alive()==False:
                                progressbar.stop()
                                break

                start_foo_thread()
            i=i+1   
        progress_window.destroy()
        copy_window.destroy()
        main_window.deiconify()

快速建议,而无需阅读所有代码,但我略读了一下,却没有看到。 每当进度条增加时,请尝试更新根窗口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM