Comprehensive Guide to PyQt5 GUI Development, Installation, and Packaging
This article provides a detailed overview of Python GUI frameworks, introduces PyQt5 and its modules, walks through installation, demonstrates basic and weather‑app examples, and explains how to package the application into an executable using tools such as fbs, PyInstaller, and related utilities.
Common GUI Frameworks
Python offers several GUI libraries, including PyQt5 (Qt bindings), Pyside6 (official Qt Python bindings), Tkinter (built‑in), PySimpleGUI (wrapper around Tkinter), WxPython, Wax, Kivy, BeeWare, Toga, Eel, Flexx, pywebview, and enaml. The author plans to start with PyQt5 due to its abundant resources.
PyQt5 Introduction
PyQt is the Python implementation of the Qt framework, providing a powerful set of widgets that closely mirror Qt's API. It is available under GPL and commercial licenses, and includes modules such as QtCore, QtGui, QtWidgets, QtMultimedia, QtBluetooth, QtNetwork, QtPositioning, QtWebSockets, QtWebKit, QtXml, QtSvg, QtSql, and QtTest.
PyQt5 Installation
The author uses Python 3.6.8, PyCharm, and PyQt5. Installation commands:
pip install pyqt5
pip install pyqt5-toolsIf a dependency conflict occurs (e.g., qt5-tools 5.15.2.1.2 has requirement click~=7.0 ), resolve it with:
pip install click~=7.0Qt Designer Configuration
Qt Designer is used for drag‑and‑drop UI design. The layout includes a Widget Box, MainWindow canvas, Object Inspector, Property Editor, and Resource Browser. The generated .ui file is XML and can be converted to Python with pyuic5 .
PyQt5 Usage Example
A minimal window example:
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_())Common widgets such as QLabel, QComboBox, QCheckBox, QRadioButton, QPushButton, QTableWidget, QLineEdit, QSlider, and QProgressBar are displayed.
Button and Event Handling
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
def click():
print("Hy Button is clicked!")
app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(400, 400, 400, 300)
win.setWindowTitle("Pyqt5 Tutorial")
button = QPushButton(win)
button.resize(200, 100)
button.setText("Hi! Click Me")
button.move(100, 100)
button.clicked.connect(click)
win.show()
sys.exit(app.exec_())Practical Project – Simple Weather Query App
The UI is designed with Qt Designer (Weather.ui) containing a ComboBox for city selection, a TextEdit for output, and Query/Clear buttons. The UI is converted to Python with pyuic5 and integrated into a MainDialog class that fetches weather data via the AMap API.
import sys
import Weather
from PyQt5.QtWidgets import QApplication, QDialog
import requests
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("https://restapi.amap.com/v3/weather/weatherInfo?key=f4fd5b287b6d7d51a3c60fee24e42002&city={}".format(cityCode))
if r.status_code == 200:
data = r.json()['lives'][0]
weatherMsg = '城市:{}\n天气:{}\n温度:{}\n风向:{}\n风力:{}\n湿度:{}\n发布时间:{}\n'.format(
data['city'], data['weather'], data['temperature'], data['winddirection'], data['windpower'], data['humidity'], data['reporttime'])
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__':
myapp = QApplication(sys.argv)
myDlg = MainDialog()
myDlg.show()
sys.exit(myapp.exec_())Packaging the Application
Python freezing tools such as py2exe, PyInstaller, and fbs can create executables. The author chooses fbs (built on PyInstaller) for its convenience.
pip install fbs
fbs startproject
# follow prompts to set project name, etc.
# move demo.py and Weather.py into src/main/python, rename demo.py to main.py, add:
from fbs_runtime.application_context.PyQt5 import ApplicationContext
fbs freezeThe resulting executable is placed in \target\MyApp .
References
Links to the official documentation of PyQt5, Qt, Pyside6, Tkinter, PySimpleGUI, WxPython, Wax, Kivy, BeeWare, Toga, Eel, Flexx, pywebview, enaml, and fbs are provided.
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.