【发布时间】:2016-11-21 09:20:01
【问题描述】:
我想要一个向用户显示下载进度的进度条。更新GUI并同时下载时进度条冻结,我明白为什么但我不知道如何解决它。我使用这篇文章尝试了多线程: Tkinter: How to use threads to preventing main event loop from “freezing” 并使用 The Basics of Python Multithreading and Queues 作为指导来帮助我满足我的需求。问题是我尝试实现目标的方式,当我改变它来做我需要它做的事情时,我似乎总是犯错误。
我的代码的最基本版本(没有多线程):
from Tkinter import *
import ttk
from urllib import URLopener # Downloading files
# Make frame to tell user what file is getting downloaded
self.Progressmsg = Label(self, text="TempValue")
self.Progressmsg.pack(pady=(10,0))
# Make progress bar to show user download progress
self.Progressbar = ttk.Progressbar(self, mode="determinate", orient='horizontal', lengt=280, maximum=len(self.AllClasses))
self.Progressbar.pack(padx=10, pady=10)
self.Progressbar["value"] = 0
def DownloadFile(Class):
# Update progress message
self.Progressmsg["text"] = "Downloading {0}.ics...".format(Class)
# Download each file from saxion website
CalFile = URLopener()
CalFile.retrieve("http://[school website]/ical/group/{0}.ics".format(Class), "Data/{0}.ics".format(Class))
# Update progress bar
self.Progressbar["value"] += 1
for Study in self.Parameters["Classes"]:
for Class in Study:
DownloadFile(Class)
注意:在此代码中,AllClasses 是必须从中下载日历文件的不同类的列表。
代码本身是我没有包含的一个相当大的类的一部分。这就是为什么我使用 self.[variablename]
当此代码运行时,进度条不会加载或更新,所有文件都会正确下载,下载后进度条会立即更新所有文件。我的问题是:我该如何解决这个问题?
【问题讨论】:
标签: python multithreading tkinter ttk