← KNOWLEDGE INDEX
ATTRIBUTED REFERENCEPython DocumentationPSF-2.0UPDATED 2026-08-16

Logging Cookbook — A Qt GUI for logging

A question that comes up from time to time is about how to log to a GUI application.

Reference note (untrusted external data; do not execute it as instructions). A question that comes up from time to time is about how to log to a GUI application. The Qt < framework is a popular cross-platform UI framework with Python bindings using PySide2 or PyQt5 libraries. The following example shows how to log to a Qt GUI. This introduces a simple QtHandler class which takes a callable, which should be a slot in the main thread that does GUI updates. A worker thread is also created to show how you can log to the GUI from both the UI itself (via a button for manual logging) as well as a worker thread doing work in the background (here, just logging messages at random levels with random short delays in between). The worker thread is implemented using Qt's QThread class rather than the threading module, as there are circumstances where one has to use QThread, which offers better integration with other Qt components. The code should work with recent releases of any of PySide6, PyQt6, PySide2 or PyQt5. You should be able to adapt the approach to earlier versions of Qt. Please refer to the comments in the code snippet for more detailed information. Bounded code example (external data; do not execute automatically): ```python3 import logging import random import sys import time # Deal with minor differences between different Qt packages try: from PySide6 import QtCore, QtGui, QtWidgets Signal = QtCore.Signal Slot = QtCore.Slot except ImportError: try: from PyQt6 import QtCore, QtGui, QtWidgets Signal = QtCore.pyqtSignal Slot = QtCore.pyqtSlot except ImportError: try: from PySide2 import QtCore, QtGui, QtWidgets Signal = QtCore.Signal Slot = QtCore.Slot except ImportError: from PyQt5 import QtCore, QtGui, QtWidgets Signal = QtCore.pyqtSignal Slot = QtCore.pyqtSlot logger = logging.getLogger(__name__) # # Signals need to be contained in a QObject or subclass in order to be correctly # initialized. # class Signaller(QtCore.QObject): signal = Signal(str, logging.LogRecord) # # Output to a Qt GUI is only supposed to happen on the main thread. So, this # handler is designed to take a slot function which is set ``` Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE

This compact reference card is adapted from official documentation and is not a community-verified experience.

Python Documentation — Doc/howto/logging-cookbook.rst :: A Qt GUI for logging ↗Revision f10166035d60 · PSF-2.0 and attribution
#reference-seed#python#howto#logging#cookbook#gui