2011-05-22 100 views
1

所以我的代码:线程问题

from Tkinter import * 
from urllib2 import * 
import thread 
Admin = Tk() 
def download(): 
    def dwnload(): 
     def Listen(): 
       os.startfile(filepath) 

     chunks = 100000 
     dat = '' 
     runum = runums.replace(' ', '%20') 
     song = '3 rounds and a sound' 
     url = 'http://bonton.sweetdarkness.net/music/Blind%20Pilot%20--%203%20Rounds%20and%20A%20Sound.mp3' 
     down = urlopen(url) 
     downso = 0 
     tota = down.info().getheader('Content-Length').strip() 
     tota = int(tota) 
     while 1: 
      a = down.read(chunks) 
      downso += len(a) 

      if not a: 
       break 
      dat += a 
      percent = float(downso)/tota 
      percent = round(percent*100, 1) 

      sys.stdout.write(str(percent)) 
      sys.stdout.flush() 

      sys.stdout.write("\b"*4) 


     filepath = 'C:\\WhaleWire\\Downloaded\\' + song + '.mp3' 

     local = open(filepath, "wb") 
     local.write(dat) 
     print '1Done' 
    thread.start_new_thread(dwnload,()) 
button = Button(Admin, text='Download', command=download) 
button.pack() 
button = Button(Admin, text='Download', command=download) 
button.pack() 
Admin.mainloop() 

当我按下的下载按钮任我得到一个错误:Unhandled exception in thread started by <function dwnload at 0x00000000029D4C88>

+0

是否有关于未处理异常的任何细节? – icktoofay 2011-05-22 04:06:45

+0

没错,唯一的详细信息是在错误中得到的。 – 2011-05-22 04:08:26

+0

你确定这是一个线程问题吗?如果你不用线程调用'dwnload()'函数会发生什么? – Johnsyweb 2011-05-22 04:11:25

回答

1

线程是一种低层次的接口线,我会建议使用threading,这实际上有有意义的例外。而且,确保Tkinter和你的第二个线程之间绝对没有收敛,因为锁定是一件非常非常痛苦的事情。

我可能需要锁定download函数。