Comprehensive Guide to Python GUI Development with PyQt5 and Related Frameworks
This article provides an extensive overview of Python GUI development, covering popular frameworks such as PyQt5, PySide6, Tkinter, and others, detailing installation steps, code examples, UI design with Qt Designer, and packaging techniques using tools like fbs to create standalone desktop applications.
Common GUI Frameworks
Python offers a variety of GUI frameworks including PyQt5 (Qt bindings with over 620 classes and 6000 functions), PySide6 (official Qt Python bindings for Qt6), Tkinter (built‑in TCL‑based GUI), PySimpleGUI (wrapper for Tkinter), WxPython, Wax, Kivy, BeeWare, Toga, Eel, Flexx, pywebview, and enaml.
Personal recommendation: start with PyQt5 due to abundant resources, then explore PySide6 and PySimpleGUI as needed.
PyQt5 Introduction
PyQt is the Python binding for the Qt C++ library, providing a rich set of cross‑platform widgets and a signal‑slot mechanism. It is available under GPL and commercial licenses.
High‑performance Qt widget set
Cross‑platform (Linux, Windows, macOS)
Signal‑slot communication
Full Qt encapsulation
IDE‑generated UI code
Comprehensive widget collection
Key modules include QtCore, QtGui, QtWidgets, QtMultimedia, QtBluetooth, QtNetwork, QtPositioning, Enginio, QtWebSockets, QtWebKit, QtWebKitWidgets, QtXml, QtSvg, QtSql, and QtTest.
Installing PyQt5
Using Python 3.6.8 (compatible with fbs), install the required packages:
<code>pip install pyqt5
pip install pyqt5-tools</code>If a dependency conflict occurs (e.g., click version), resolve it with:
<code>pip install click~=7.0</code>Qt Designer Configuration
Qt Designer allows drag‑and‑drop UI design. The interface consists of a Widget Box (components), a MainWindow canvas, an Object Inspector (UI hierarchy), a Property Editor (widget properties), and a Resource Browser (assets).
After designing, the .ui file (XML) can be used directly or converted to Python with pyuic5 or pyrcc5 .
PyQt5 Usage Example
Creating a simple window:
<code>import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(400, 400, 400, 300)
win.setWindowTitle("Pyqt5 Tutorial")
win.show()
sys.exit(app.exec_())</code>Adding a label:
<code>label = QLabel(win)
label.resize(200, 100)
label.setText("Hi this is Pyqt5")
label.move(100, 100)</code>Button with click event:
<code>def click():
print("Hy Button is clicked!")
button = QPushButton(win)
button.resize(200, 100)
button.setText("Hi! Click Me")
button.move(100, 100)
button.clicked.connect(click)</code>Practical Project: Simple Weather App
Design the UI with Qt Designer (buttons, ComboBox, TextEdit), then convert the .ui file to Python using pyuic5 . Implement business logic in a separate class that queries weather data via the AMap API and updates the TextEdit.
<code>import sys, requests
from PyQt5.QtWidgets import QApplication, QDialog
import Weather
class MainDialog(QDialog):
def __init__(self, parent=None):
super(QDialog, self).__init__(parent)
self.ui = Weather.Ui_Dialog()
self.ui.setupUi(self)
def queryWeather(self):
cityName = self.ui.comboBox.currentText()
cityCode = self.getCode(cityName)
r = requests.get(f"https://restapi.amap.com/v3/weather/weatherInfo?key=YOUR_KEY&city={cityCode}")
if r.status_code == 200:
data = r.json()['lives'][0]
weatherMsg = f"城市:{data['city']}\n天气:{data['weather']}\n温度:{data['temperature']}\n..."
else:
weatherMsg = '天气查询失败,请稍后再试!'
self.ui.textEdit.setText(weatherMsg)
def getCode(self, cityName):
cityDict = {"北京": "110000", "苏州": "320500", "上海": "310000"}
return cityDict.get(cityName, '101010100')
def clearText(self):
self.ui.textEdit.clear()
if __name__ == '__main__':
app = QApplication(sys.argv)
dlg = MainDialog()
dlg.show()
sys.exit(app.exec_())</code>Packaging the Application
Freezing Python scripts into executables can be done with PyInstaller, py2exe, cx_Freeze, etc. This guide uses fbs , a wrapper around PyInstaller that simplifies PyQt5 packaging.
<code>pip install fbs
fbs startproject
# Follow prompts to set app name, then place your .py files in src/main/python
# Replace main.py with your demo script and add:
from fbs_runtime.application_context.PyQt5 import ApplicationContext
fbs freeze</code>The resulting executable is located in \target\MyApp .
References
Links to official documentation for PyQt5, Qt, PySide6, Tkinter, WxPython, Kivy, BeeWare, Toga, Eel, Flexx, pywebview, enaml, and fbs.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.