Hi I made a music downloading program that works great it tells you the percent its done and then if i move the window at all it stops downloading . I made a diffrent little script that downloads a specified mp3 on the web and i can move it as much as i want and it doesent stop.
the only reason im not putting up the code is that it is really long. Its around 1500 lines. Here is the small script that i made to download one file.
does any one know why it stops the program from working? the little script:
from Tkinter import *
from urllib2 import *
admin = Tk()
Admin = Tk()
listbox = Listbox(admin, bg="PURPLE")
listbox.pack()
def __init__(self, master):
def replay():
Admin.destroy()
os.system('WhaleWire.exe')
frame = Frame(master)
frame.pack()
image1 = PhotoImage(file="whalewire.gif")
w = image1.width()
h = image1.height()
master.geometry("%dx%d+0+0" % (w, h))
# tk.Frame has no image argument
panel1 = Label(master, image=image1)
panel1.pack(side='top', fill='both', expand='yes')
panel1.image = image1
self.e = Entry(frame)
self.e.grid(row=0, column=0)
b = Button(frame, text='Search', command=self.whale)
b.grid(row=0, column=1)
def library():
path = 'C:\WhaleWire\Downloaded'
aw=[]
for infile in glob.glob( os.path.join(path,'*.mp3') ):
libr = infile.split('Downloaded',1)
aw.append('\n')
aw.append(infile)
la = Label(Admin,width=100,height=50, text=aw).grid(row=0,column=7)
b2s = Button(Admin,text='Search', command=replay).grid(row=0,column=8)
b11 = Button(frame, text='Library', command=library)
b11.grid(row=0, column=3)
def fores():
chunks = 10000
dat = ''
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)
listbox.insert(END, percent)
listbox.update()
listbox.delete(0, END)
listbox.insert(END, percent)
listbox.update()
button = Button(Admin, text='Download', command=fores)
button.pack()
button = Button(Admin, text='Download'开发者_运维技巧, command=fores)
button.pack()
mainloop()
Most likely the problem is because you are calling update
. You should never do that unless you know for certainty what the ramifications are. update
causes a new event loop to be entered. Essentially, you end up with an infinite loop inside an infinite loop.
Try changing your update
to update_idletasks
and see if that solves your problem. This variation of update
only processes "idle" events such as screen redraws and is considerably less likely to cause problems.
Also, you definitely don't need "update; insert; delete; update". That won't have any noticeable effect. A single call to update_idletasks
after the delete is sufficient.
Finally, you can avoid the use of update_idletasks
completely by rearranging your code. Write a function that reads a single chunk of data and updates the progress bar. Then, if it hasn't reached EOF, use after
to call that function again a few milliseconds later. When it reaches EOF it stops calling itself. Doing this means you don't have to create your own potentially infinite loop, and the event loop is guaranteed to be entered once per iteration. Once this EOF is detected you can then call a function (again using after
) to do any final processing.
精彩评论