线程问题

时间:2011-05-22 04:05:08

标签: python tkinter

所以我有代码:

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>

1 个答案:

答案 0 :(得分:1)

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

我可能需要锁定download函数。