Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since tkinter is <em>single threaded</em>, you need another thread to execute your <code>main</code> function without freezing the GUI. One common approach is that the working thread puts the messages into a synchronized object (like a <a href="http://docs.python.org/2/library/queue.html"><code>Queue</code></a>), and the GUI part consumes this messages, updating the progress bar.</p> <p>The following code is based on a full detailed <a href="http://code.activestate.com/recipes/82965/">example</a> on ActiveState:</p> <pre><code>import tkinter as tk from tkinter import ttk import threading import queue import time class App(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.queue = queue.Queue() self.listbox = tk.Listbox(self, width=20, height=5) self.progressbar = ttk.Progressbar(self, orient='horizontal', length=300, mode='determinate') self.button = tk.Button(self, text="Start", command=self.spawnthread) self.listbox.pack(padx=10, pady=10) self.progressbar.pack(padx=10, pady=10) self.button.pack(padx=10, pady=10) def spawnthread(self): self.button.config(state="disabled") self.thread = ThreadedClient(self.queue) self.thread.start() self.periodiccall() def periodiccall(self): self.checkqueue() if self.thread.is_alive(): self.after(100, self.periodiccall) else: self.button.config(state="active") def checkqueue(self): while self.queue.qsize(): try: msg = self.queue.get(0) self.listbox.insert('end', msg) self.progressbar.step(25) except Queue.Empty: pass class ThreadedClient(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue def run(self): for x in range(1, 5): time.sleep(2) msg = "Function %s finished..." % x self.queue.put(msg) if __name__ == "__main__": app = App() app.mainloop() </code></pre> <p>Since the <a href="http://code.activestate.com/recipes/82965/">original example</a> on ActiveState is a bit messy IMO (the <code>ThreadedClient</code> is quite coupled with the <code>GuiPart</code>, and things like controlling the moment to spawn the thread from the GUI are not as straightforward as they could be), I have refactored it and also added a Button to start the new thread.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload