我想根据名称(http://mysite.com/id?id=abc1
,http://mysite.com/id?id=abc2
,...)从远程服务器下载图片。
下载部分有效,但由于可能有很多文件需要下载(有时超过200个),我想添加一个标签“Please wait”(Tkinter)。问题是我的标签只在下载完成时出现,当然这不是很有用。
我怎样才能让它早点出现?
from Tkinter import *
import urllib2
import time
window = Tk()
window.title('My Downloader')
photo=PhotoImage(file="logo.gif")
labl = Label(window, image=photo)
labl.pack()
mainid = Label(window, text = 'Main id:')
mid = Entry(window)
complete = Label(window, text = 'download complete !')
def dl():
right = Label (window, text ='downloading...Please wait')
right.pack()
time.sleep(2)
i='10'
while int(i) <= 100000 :
if len(str(i))==1:
i="0000"+str(i)
elif len(str(i))==2:
i="000"+str(i)
elif len(str(i))==3:
i="00"+str(i)
elif len(str(i))==4:
i="0"+str(i)
url = 'http://mysite.com/id?recordID='+ str(mid.get())+str(i)
try:
dltest = urllib2.urlopen(url)
with open(str(mid.get())+str(i)+'.tiff', 'wb') as f:
while True:
content = dltest.read()
if not content: break
f.write(content)
except urllib2.URLError:
if int(i)==10:
wrong = Label (window, text ='wrong id ')
wrong.pack()
break
else:
complete.pack()
break
i=int(i)+10
dlbutton = Button(window, text =' Download!', command=dl)
mainid.pack()
mid.pack()
dlbutton.pack()
window.mainloop()
答案 0 :(得分:2)
设置标签后,只需调用window.update()。
right = Label (window, text ='downloading...Please wait')
right.pack()
window.update()
但更好的办法是将URL下载线程化。这可能是一项相当复杂的任务,但允许您在下载文件时释放GUI。请查看this page,了解如何编写程序。