Это старый вопрос, но рецепт кода, на который я ссылаюсь, помог мне с подобной концепцией, поэтому я подумал, что им следует поделиться.
В этом типе проблемы необходимо использовать многопоточность, чтобы мы ускорили работу по обновлению графического интерфейса и выполнению реальной задачи (например, отправка электронных писем). Взгляните на этот кодовый рецепт из Active State, я считаю, что это именно то, что вы ищете в качестве примера потоков и передачи информации между потоками (через очередь).
Я пытаюсь выделить важные части из рецепта кода. Я не включаю настройку самого индикатора выполнения, а скорее общую структуру кода и получение / установку очереди.
import Tkinter
import threading
import Queue
class GuiPart:
def __init__(self, master, queue, endCommand):
self.queue = queue
# Do GUI set up here (i.e. draw progress bar)
# This guy handles the queue contents
def processIncoming(self):
while self.queue.qsize():
try:
# Get a value (email progress) from the queue
progress = self.queue.get(0)
# Update the progress bar here.
except Queue.Empty:
pass
class ThreadedClient:
# Launches the Gui and does the sending email task
def __init__(self, master):
self.master = master
self.queue = Queue.Queue()
# Set up the Gui, refer to code recipe
self.gui = GuiPart(master, self.queue, ...)
# Set up asynch thread (set flag to tell us we're running)
self.running = 1
self.email_thread = threading.Thread(target = self.send_emails)
self.email_thread.start()
# Start checking the queue
self.periodicCall()
def periodicCall(self):
# Checks contents of queue
self.gui.processIncoming()
# Wait X milliseconds, call this again... (see code recipe)
def send_emails(self): # AKA "worker thread"
while (self.running):
# Send an email
# Calculate the %age of email progress
# Put this value in the queue!
self.queue.put(value)
# Eventually run out of emails to send.
def endApplication(self):
self.running = 0
root = Tkinter.Tk()
client = ThreadedClient(root)
root.mainloop()