Frontend Development 15 min read

Comprehensive Guide to PyQt5: GUI Frameworks, Installation, Usage Examples, and Packaging

This tutorial introduces common Python GUI frameworks, provides an in‑depth overview of PyQt5 and its modules, walks through installation, Qt Designer configuration, code examples for creating windows and handling events, demonstrates a weather‑query application, and explains how to package the program into a standalone executable.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Comprehensive Guide to PyQt5: GUI Frameworks, Installation, Usage Examples, and Packaging

Introduction Python is increasingly used for desktop GUI development, though web‑based interfaces are more popular; this guide focuses on PyQt5 and other Python GUI toolkits.

Common GUI Frameworks Popular options include PyQt5 (Qt bindings), PySide6, Tkinter, PySimpleGUI, WxPython, Kivy, BeeWare, Toga, Eel, Flexx, pywebview, and enaml, each with different strengths and platform support.

PyQt5 Overview PyQt5 is a Python binding for Qt5, offering over 620 classes and 6000 functions. It provides GPL and commercial licenses, supports Linux, Windows, and macOS, and includes modules such as QtCore, QtGui, QtWidgets, QtMultimedia, QtNetwork, QtWebKit, QtSql, and more.

Installation Install PyQt5 and the Qt Designer helper with:

pip install pyqt5
pip install pyqt5-tools

If a dependency conflict occurs (e.g., click version), resolve it with:

pip install "click~=7.0"

Qt Designer Configuration In PyCharm, add an external tool named QtDesigner pointing to the designer executable and set the working directory to $FileDir . Similarly configure PyUIC and PyRCC to convert .ui and .qrc files to Python modules.

Basic PyQt5 Example Creating a blank window:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
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 illustrated with screenshots.

Button and Event Handling

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton

def click():
    print("Hi 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_())

Weather Query Application (Practical Project) The project uses Qt Designer to create a UI with ComboBox, TextEdit, and two buttons (Query and Clear). After converting Weather.ui to Weather.py with pyuic5 , a MainDialog class adds business logic that fetches weather data from the AMap API and displays it.

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):
        city = self.ui.comboBox.currentText()
        code = self.getCode(city)
        r = requests.get(f"https://restapi.amap.com/v3/weather/weatherInfo?key=YOUR_KEY&city={code}")
        if r.status_code == 200:
            data = r.json()['lives'][0]
            msg = (f"城市:{data['city']}\n天气:{data['weather']}\n温度:{data['temperature']}\n"
                   f"风向:{data['winddirection']}\n风力:{data['windpower']}\n湿度:{data['humidity']}\n发布时间:{data['reporttime']}")
        else:
            msg = "天气查询失败,请稍后再试!"
        self.ui.textEdit.setText(msg)
    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_())

Packaging the Application Freezing the script into an executable can be done with PyInstaller, py2exe, cx_Freeze, or the higher‑level fbs tool (which wraps PyInstaller). After installing fbs :

pip install fbs
fbs startproject   # follow prompts to create project structure
# 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" at the top of main.py
fbs freeze   # produces a standalone .exe in target/MyApp

The guide concludes with screenshots of the final executable and links to the original article.

packagingDesktop ApplicationPyQt5Weather APIPython GUIQt Designerfbs
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.