嗨,所以我的代码有问题(可能不是urllib2本身),但它不会创建进度条并崩溃。但在等待代码完成后下载我的文件。有没有我可以阻止挂起,并可能分解成更小的块下载,因为我没有什么经验与python。。。我的代码如下:
def iPod1():
pb = ttk.Progressbar(orient ="horizontal",length = 200, mode ="indeterminate")
pb.pack(side="top")
pb.start()
download = "http://downloads.sourceforge.net/project/whited00r/7.1/Whited00r71-iPodTouch1G.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fwhited00r%2Ffiles%2F7.1%2F&ts=1405674672&use_mirror=softlayer-ams"
request = urllib2.urlopen( download)
pb.start()
output = open("Whited00r71-iPodTouch1G.zip", "wb")
output.write(request.read())
output.close()
pb.stop
tkMessageBox.showinfo(title="Done", message="Download complete. Please follow the installation instructions provided in the .html file.")
通过将下载移动到自己的线程中,可以避免挂起/冻结GUI。由于GUI不能从运行Tk mainloop的线程以外的其他线程更改,因此我们必须定期检查下载线程是否已完成。这通常是通过在小部件对象上使用
after()
方法重复调度延迟的函数调用来实现的。你知道吗在将整个文件写入本地文件之前不将其读入内存可以通过
shutil.copyfileobj()
完成。你知道吗相关问题 更多 >
编程相关推荐