clm42
clm42

Reputation: 65

Tkinter label update during code run

I have a tkinter window with one button and one text label. When the button is pushed the script downloads a file using FTP. I want the label to show the current download progress. The code for the download, the root window, and the progress monitor all work. The label however starts at 0 and does not update until the download is finished at which point it updates to 100. How can I make the code that the button starts run concurrently with the window mainloop() and have them communicate?

from Tkinter import *
from time import sleep
from ftplib import FTP
import os
try:
    root = Tk()
    class processor():
        def __init__(self):
            self.amount_transfered=0
            self.download_size=0

        def window(self,master):
            self.content = Frame(master)
            self.content.grid(column=0, row=0)
            self.content.master.title('Parcel Processor')

            self.downLbl=Label(self.content,text='Downloading').grid(column=0,row=0)
            self.downVar=StringVar()
            self.downVar.set('0')
            self.downPct=Label(self.content,textvariable=self.downVar).grid(column=1,row=0)
            self.start=Button(self.content, text='START', command=self.parcelDownloader).grid(column=0, row=1)

        def handleDownload(self,block):
            self.parcel_zip.write(block)
            self.downVar.set(str(((self.amount_transfered + len(block))*100)/self.download_size))
            self.amount_transfered = self.amount_transfered + len(block)

        def parcelDownloader(self):
            os.chdir(r"C:\GIS Projects\Parcel Downloads")
            ftp=FTP("FTPSITE", "User", "Pass")
            ftpdatelist=[]
            for filename in ftp.nlst():
                if filename[0:2]=='pa':
                    ftpdatelist.append(filename[:8])            
            parcels='%s.zip' % max(ftpdatelist)
            self.download_size=ftp.size(parcels)
            try:
                self.parcel_zip = open("parcels.zip", 'wb')
                ftp.retrbinary('RETR %s' % parcels, self.handleDownload, 327680)
                self.parcel_zip.close()
            except Exception as e:
                print e
                self.parcel_zip.close()
            finally:
                ftp.quit()
                self.amount_transfered=0

    processor=processor()
    processor.window(root)
    root.mainloop()

except Exception as e:
    print e
    sleep(10)

Upvotes: 2

Views: 3552

Answers (2)

rn0mandap
rn0mandap

Reputation: 333

does this help? calls every 1/10 of a second

root.after(100, lambda: label.config(text=downloadprogress))

Upvotes: 0

Louis
Louis

Reputation: 2890

You can manually force a refresh of the window-refreshing events through the loop. Use update_idletasks() methods on the toplevel Tk, and it should refresh things properly. See:

http://www.pythonware.com/library/tkinter/introduction/x9374-event-processing.htm

which talks about it a little more.

Upvotes: 4

Related Questions