作者:张大鹏
时间:2025-11-05
标签:Python、PyQt5、GUI、桌面开发、实战教程
一、前言
在桌面应用开发中,计算器 是一个非常适合入门的练手项目。
它涉及到图形界面设计、事件绑定、信号槽机制、布局管理等核心概念。
今天我们将使用 PyQt5(同样适用于 PyQt6)一步步实现一个可用的计算器程序,从 UI 布局到功能逻辑完整讲解。
最终效果如下👇:
- 支持加减乘除和小数运算;
- 按钮布局整齐;
- 可通过按钮或键盘输入操作;
- 界面美观,可打包为独立应用。
二、项目环境
| 项目依赖 | 说明 |
|---|---|
| Python 3.8+ | 推荐使用 3.9 或以上版本 |
| PyQt5 | 图形界面开发框架 |
| Qt Designer(可选) | 可视化 UI 设计工具 |
安装命令:
1pip install PyQt5 2
三、项目结构
我们将项目组织如下:
1pyqt_calculator/ 2│ 3├── main.py # 主程序入口 4├── calculator.ui # Qt Designer 设计文件(可选) 5└── ui_calculator.py # UI 转换后的 Python 文件 6
四、第一步:构建界面
我们先手动创建一个简单的界面,不依赖 Qt Designer。
1# main.py 2from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QPushButton, QLineEdit, QVBoxLayout 3from PyQt5.QtCore import Qt 4import sys 5 6 7class Calculator(QWidget): 8 def __init__(self): 9 super().__init__() 10 self.setWindowTitle("PyQt 计算器") 11 self.setFixedSize(300, 400) 12 self.init_ui() 13 14 def init_ui(self): 15 # 显示区域 16 self.display = QLineEdit() 17 self.display.setAlignment(Qt.AlignRight) 18 self.display.setReadOnly(True) 19 self.display.setStyleSheet("font-size: 24px; padding: 10px;") 20 21 # 按钮布局 22 buttons = { 23 '7': (0, 0), '8': (0, 1), '9': (0, 2), '/': (0, 3), 24 '4': (1, 0), '5': (1, 1), '6': (1, 2), '*': (1, 3), 25 '1': (2, 0), '2': (2, 1), '3': (2, 2), '-': (2, 3), 26 '0': (3, 0), '.': (3, 1), 'C': (3, 2), '+': (3, 3), 27 '=': (4, 0, 1, 4) 28 } 29 30 grid = QGridLayout() 31 for text, pos in buttons.items(): 32 button = QPushButton(text) 33 button.setStyleSheet("font-size: 20px; height: 50px;") 34 if len(pos) == 2: 35 grid.addWidget(button, pos[0], pos[1]) 36 else: 37 grid.addWidget(button, pos[0], pos[1], pos[2], pos[3]) 38 button.clicked.connect(self.on_button_clicked) 39 40 layout = QVBoxLayout() 41 layout.addWidget(self.display) 42 layout.addLayout(grid) 43 self.setLayout(layout) 44 45 def on_button_clicked(self): 46 sender = self.sender().text() 47 current = self.display.text() 48 49 if sender == "C": 50 self.display.clear() 51 elif sender == "=": 52 try: 53 result = str(eval(current)) 54 self.display.setText(result) 55 except Exception: 56 self.display.setText("Error") 57 else: 58 self.display.setText(current + sender) 59 60 61if __name__ == "__main__": 62 app = QApplication(sys.argv) 63 window = Calculator() 64 window.show() 65 sys.exit(app.exec_()) 66
运行后即可看到一个基础的计算器界面。
五、第二步:添加键盘输入支持
许多用户习惯直接使用键盘输入,我们可以重写 keyPressEvent 方法:
1def keyPressEvent(self, event): 2 key = event.key() 3 if key == Qt.Key_Return or key == Qt.Key_Enter: 4 self.on_button_clicked() # 模拟 "=" 5 elif key == Qt.Key_Backspace: 6 self.display.setText(self.display.text()[:-1]) 7 else: 8 text = event.text() 9 if text.isdigit() or text in ['+', '-', '*', '/', '.']: 10 self.display.setText(self.display.text() + text) 11
这样计算器即可同时响应键盘输入。
六、第三步:改进计算逻辑
直接使用 eval() 虽然简单,但存在安全风险。
我们可以使用 Python 内置的 ast 模块安全地解析表达式。
1import ast 2import operator as op 3 4operators = { 5 ast.Add: op.add, 6 ast.Sub: op.sub, 7 ast.Mult: op.mul, 8 ast.Div: op.truediv 9} 10 11def safe_eval(expr): 12 node = ast.parse(expr, mode='eval').body 13 def _eval(node): 14 if isinstance(node, ast.Num): 15 return node.n 16 elif isinstance(node, ast.BinOp): 17 return operators[type(node.op)](_eval(node.left), _eval(node.right)) 18 else: 19 raise TypeError(node) 20 return _eval(node) 21
替换掉 eval(current) 部分即可更安全地计算。
七、第四步:美化界面
使用 Qt Style Sheet (QSS) 进行样式美化:
1self.setStyleSheet(""" 2 QWidget { 3 background-color: #2E3440; 4 } 5 QLineEdit { 6 background: #3B4252; 7 color: #ECEFF4; 8 border: none; 9 border-radius: 5px; 10 } 11 QPushButton { 12 background-color: #4C566A; 13 color: #ECEFF4; 14 border-radius: 8px; 15 } 16 QPushButton:hover { 17 background-color: #5E81AC; 18 } 19 QPushButton:pressed { 20 background-color: #81A1C1; 21 } 22""") 23
界面将呈现出类似现代深色主题的视觉效果。
八、第五步:项目打包
当应用开发完成后,我们可以使用 pyinstaller 打包为可执行文件:
1pip install pyinstaller 2pyinstaller -F -w main.py 3
-F:打包为单文件-w:去掉控制台窗口(适合 GUI 应用)
生成的可执行文件在 dist/ 目录中。
九、扩展思路
这个项目的核心结构可以进一步扩展:
- ✅ 添加括号与优先级运算
- ✅ 显示历史计算记录
- ✅ 支持科学计算(sin、cos、sqrt 等)
- ✅ 国际化(i18n)
- ✅ 保存用户偏好与主题
通过这些改进,你可以将这个小项目打磨成一个完整的专业级桌面工具。
十、总结
通过本篇实战,我们掌握了以下关键技能:
- 使用 PyQt5 搭建窗口与控件布局;
- 理解 信号与槽 的事件机制;
- 构建计算逻辑与安全求值;
- 使用 QSS 美化界面;
- 打包发布桌面应用。
PyQt 的强大不仅在于能做出“好看的界面”,更在于它与 Python 的生态融合:
你可以轻松把它扩展成数据可视化工具、AI 模型控制台、图像处理前端等等。
下一步,不妨试着给这个计算器加上图表或语音输入功能,体验 PyQt 真正的扩展性。
《用 PyQt 开发一个桌面计算器:从零到完整实战指南》 是转载文章,点击查看原文。
