Python GUI Development with PyQt5: Framework Overview, Installation, and Weather App Example
This tutorial introduces Python desktop GUI development, compares common frameworks, explains PyQt5 features and installation, demonstrates basic window creation, shows a complete weather‑query application with Qt Designer and API integration, and covers packaging the app into an executable.
Python can be used to create desktop graphical applications, which is less common than web‑based GUIs but valuable for developers unfamiliar with languages like C# or WPF.
Common Python GUI frameworks include PyQt5, PySide6, Tkinter, PySimpleGUI, WxPython, Wax, Kivy, BeeWare, Toga, Eel, Flexx, pywebview, and enaml.
The author plans to start learning with PyQt5 because of its abundant resources, then explore PySide6, and finally try PySimpleGUI for simple tasks.
PyQt5 is a Python binding for the Qt C++ library, offering over 620 classes and 6000 functions, available under GPL or commercial licenses. Its key features are cross‑platform support, signal‑slot communication, full Qt encapsulation, a rich set of widgets, and IDE‑generated UI code.
Installation is performed via pip install pyqt5 and pip install pyqt5-tools . A version conflict with the click package may arise, which can be resolved by installing a compatible version: pip install click~=7.0 .
Qt Designer can be configured in PyCharm as an external tool (e.g., Name: QtDesigner, Program: D:\Program Files\Python36\Lib\site-packages\qt5_applications\Qt\bin\designer.exe, Working directory: $FileDir).
A minimal PyQt5 window is created with the following code:
<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>Widgets such as QLabel and QPushButton are demonstrated, including a button click handler:
<code>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_())</code>The tutorial then builds a simple weather‑query application: the UI is designed in Qt Designer, saved as Weather.ui , converted to Python with pyuic5 , and integrated into a dialog class that fetches weather data from the AMap API using requests . The main dialog connects the query and clear buttons to custom slots.
<code>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(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风向:{data['winddirection']}\n风力:{data['windpower']}\n湿度:{data['humidity']}\n发布时间:{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__':
app = QApplication(sys.argv)
dlg = MainDialog()
dlg.show()
sys.exit(app.exec_())</code>Running demo.py displays the weather information for the selected city.
To distribute the application as a Windows executable, the tutorial recommends using the fbs tool (built on PyInstaller). After installing pip install fbs , a project is created with fbs startproject , the source files are placed in src/main/python , and the application is frozen with fbs freeze , producing an .exe in the target directory.
At the end of the article a QR code is provided for free Python learning resources, which is promotional but does not affect the instructional content.
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.