Desktop GUIs in Python: Tkinter, PyQt5, and thread boundaries
Lessons from building desktop utilities across two graphical toolkits: event loop starvation, background workers, signals, and native distribution.
On this page
The first version of Youtube-Downloader worked fine for short videos. Enter a URL, click Download, the file appears. I thought I was done.
Then I tried a longer video. The window went white. The title bar showed "Not Responding." I could move the mouse but clicking anything had no effect. The download was happening — I could see the file growing on disk — but the application had locked up.
That's not a bug in the download code. It's a bug in every desktop application that does real work inside a button callback.
Why the window freezes
A GUI runs on a loop. The operating system sends events — mouse clicks, keyboard input, repaint requests — and the application processes them one at a time. The loop needs to keep running, or the window stops responding to anything.
OS event queue
-> main thread picks up next event
-> calls the registered callback
-> repaints any changed regions
-> back to waitingWhen a button callback starts downloading a 200MB video over HTTP, the main thread is busy with that download. The event queue fills up with paint requests and click events that never get processed. The OS notices the application hasn't responded to its heartbeat and flags it "Not Responding."
The download eventually finishes, the callback returns, and the loop resumes — but the window was frozen the whole time.
# This is the problem
def on_download_clicked():
stream.download(output_path="downloads/") # blocks for 30 seconds
status_label.setText("Done!")The fix: move the work off the main thread
The main thread should do one thing: process events. Any real work — network requests, file I/O, audio decoding — belongs somewhere else.
Both Tkinter and PyQt5 support threading, but their approaches to communicating back to the UI are different.
PyQt5: signals across thread boundaries
PyQt5 has a signals-and-slots system designed exactly for this. A worker that runs in a background thread declares signals. When something happens — progress updates, completion, failure — it emits a signal. Qt routes that signal to the main thread automatically.
from PyQt5.QtCore import QThread, pyqtSignal
class DownloadWorker(QThread):
progress = pyqtSignal(int)
finished = pyqtSignal(str)
failed = pyqtSignal(str)
def __init__(self, url, output_dir):
super().__init__()
self.url = url
self.output_dir = output_dir
def run(self):
try:
def on_progress(stream, chunk, bytes_remaining):
pct = int(((stream.filesize - bytes_remaining) / stream.filesize) * 100)
self.progress.emit(pct)
stream = fetch_stream(self.url, on_progress)
path = stream.download(self.output_dir)
self.finished.emit(path)
except Exception as e:
self.failed.emit(str(e))In the main window, connecting those signals to widget methods is straightforward:
self.worker = DownloadWorker(url, "downloads/")
self.worker.progress.connect(self.progress_bar.setValue)
self.worker.finished.connect(self.on_done)
self.worker.failed.connect(self.on_error)
self.worker.start()The important thing is that you're never calling widget methods from the worker thread directly. The signal goes through Qt's internal message queue, and the slot runs on the main thread when the event loop gets back to it.
Tkinter: queues and polling
Tkinter doesn't have signals. The closest equivalent is a standard queue.Queue and root.after().
root.after(ms, fn) schedules a function to be called after a delay, without blocking. It's how Tkinter does anything asynchronous.
import tkinter as tk
import threading
import queue
class MusicPlayerUI:
def __init__(self, root):
self.root = root
self.q = queue.Queue()
self.poll()
def scan_library(self, folder):
threading.Thread(
target=self._scan_worker,
args=(folder,),
daemon=True
).start()
def _scan_worker(self, folder):
for track in find_audio_files(folder):
self.q.put(("track", track))
self.q.put(("done", None))
def poll(self):
try:
while True:
kind, data = self.q.get_nowait()
if kind == "track":
self.playlist.insert(tk.END, data)
elif kind == "done":
self.status.set("Ready")
except queue.Empty:
pass
self.root.after(50, self.poll)The worker puts results on the queue. The main thread checks the queue every 50ms, stays responsive between checks, and updates the UI with whatever it finds. It's more manual than Qt's signals, but it works without any additional libraries.
Building layouts: pack() versus Qt Designer
One other difference worth noting: how you assemble the interface.
Tkinter uses geometry managers — pack(), grid(), place() — written in Python. The layout lives in code.
frame = tk.Frame(root, padx=10, pady=10)
frame.pack(fill=tk.BOTH, expand=True)
tk.Label(frame, text="URL:").pack(side=tk.LEFT)
tk.Entry(frame).pack(side=tk.LEFT, fill=tk.X, expand=True)It's self-contained and straightforward for small interfaces. For anything with many nested panels, the structure gets hard to follow.
PyQt5 offers Qt Designer: a visual editor that saves layouts as XML .ui files. You can compile them to Python classes or load them at runtime:
from PyQt5 import uic
class App(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi("window.ui", self)
self.downloadButton.clicked.connect(self.start)The layout and the behavior stay separate. Restyling with QSS (Qt's CSS equivalent) doesn't require touching any Python.
For a quick utility that only you'll use, Tkinter is fine. For something you're distributing to other people, the polish difference becomes noticeable.
Browse Youtube-Downloader, Music-Player, and Notepad on GitHub.