开发者

Threading question

so i have the code :

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 = Butt开发者_开发百科on(Admin, text='Download', command=download)
button.pack()
Admin.mainloop()

When i press either of of the download buttons i get a error: Unhandled exception in thread started by <function dwnload at 0x00000000029D4C88>


thread is a low-level interface to threads, I would suggest using threading, which actually has meaningful exceptions. And, make sure there is absolutely no convergence between Tkinter and your second thread, because locking is a very, very, painful thing.

I thing you may need locking in the download function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜