代码运行期间 Tkinter 标签更新
我有一个 tkinter 窗口,其中有一个按钮和一个文本标签。当按下按钮时,脚本将使用 FTP 下载文件。我希望标签显示当前的下载进度。下载代码、根窗口和进度监视器都可以工作。然而,标签从 0 开始,直到下载完成后才会更新,此时它更新为 100。如何使按钮启动的代码与窗口 mainloop() 同时运行并让它们进行通信?
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过以下方式手动强制刷新窗口刷新事件
循环。在顶层 Tk 上使用 update_idletasks() 方法,它
应该正确刷新事物。请参阅:
http://www.pythonware.com/library/tkinter /introduction/x9374-event-processing.htm
对此进行了更多讨论。
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.
这有帮助吗?每 1/10 秒调用一次
does this help? calls every 1/10 of a second