Answer the question
In order to leave comments, you need to log in
Python Tkinter. Why does the GUI freeze?
Hello! Help solve the problem, please.
Wrote this code:
# -*- coding: utf-8 -*-
from Tkinter import *
import urllib, ttk, tkMessageBox
from threading import Thread
def downloader():
urllib.urlretrieve('http://cs521111v4.vk.me/u176613573/audios/d622212d34bf.mp3','Big K.R.I.T. – Bigger Picture.mp3')
th=Thread(target=downloader,args=())
def starter(event):
th.start()
pb.pack()
pb.start()
root= Tk()
pb = ttk.Progressbar(length = 200, orient = 'horizontal', mode = 'indeterminate')
but = Button(root, text = 'Go!')
root.minsize(width = 400, height = 350)
but.bind('<Button-1>', starter)
but.pack()
root.mainloop()
def downloader():
urllib.urlretrieve('http://cs521111v4.vk.me/u176613573/audios/d622212d34bf.mp3','Big.mp3')
tkMessageBox.showinfo('Done')
Answer the question
In order to leave comments, you need to log in
# -*- coding: utf-8 -*-
from Tkinter import *
import urllib, ttk, tkMessageBox
from threading import Thread
from Queue import Queue
queue = Queue() # создаём очередь
def downloader():
urllib.urlretrieve('http://cs521111v4.vk.me/u176613573/audios/d622212d34bf.mp3', 'Big K.R.I.T. – Bigger Picture.mp3')
queue.put(True) # помещаем в очередь True, после завершения загрузки. В очередь можно помещать любой объект.
th = Thread(target=downloader, args=())
def starter(event):
th.start()
pb.pack()
pb.start()
root = Tk()
# создаём задачу, которая раз в секунду будет проверять очередь
def task():
try:
q = queue.get_nowait() # получить значение из очереди
except: # если в очереди ничего нет, то возвращаем False
q = False
if q: # если вернулось True, то сообщаем об окончании
tkMessageBox.showinfo('Done')
root.after(1000, task) # снова перезапускаем задачу после выполнения
root.after(1000, task) # инициализация задачи
pb = ttk.Progressbar(length=200, orient='horizontal', mode='indeterminate')
but = Button(root, text = 'Go!')
root.minsize(width=400, height=350)
but.bind('<Button-1>', starter)
but.pack()
root.mainloop()
You cannot call Tkinter methods from threads other than the main thread (where root = Tk()).
There are two options here:
1. Try using mtTkinter.
2. Pass information about the completion of the download from the child thread to the main thread using Queue.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question