推动门户网站建设不断优化升级,婚礼摄影作品网站,手机网页翻译,车牌照损坏在网站做的能用吗目录
一、环境配置#xff1a;
二、代码实现
三、主程序
四、总结 本文使用PyQt5设计一款简单的计算器#xff0c;可以通过界面交互实现加减乘除的功能#xff0c;希望能够给初学者一些帮助。主要涉及的知识点有类的定义与初始化、类的成员函数、pyqt5的信号与槽函数等。…目录
一、环境配置
二、代码实现
三、主程序
四、总结 本文使用PyQt5设计一款简单的计算器可以通过界面交互实现加减乘除的功能希望能够给初学者一些帮助。主要涉及的知识点有类的定义与初始化、类的成员函数、pyqt5的信号与槽函数等。
具体界面如下 一、环境配置
使用pip指令安装pyqt5此处选择5.12.0版本因为笔者安装的spyder版本为4.1.5过高的版本不兼容
pip install PyQt55.12.0 -i https://pypi.douban.com/simple
pip install PyQt5-tools -i https://pypi.douban.com/simple
pip install PyQt5designer -i https://pypi.douban.com/simple
二、代码实现
1、引入依赖库。
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout, QLineEdit, QPushButton
from PyQt5.QtCore import Qt
2、定义计算器Calculator类成员函数的功能如下表所示。
函数名称函数功能 __init__() 初始化函数初始化窗口名称、尺寸、按钮名称、位置、信号响应函数等。 button_click(self, number) 选择数字与小数点 button_clear(self) 实现屏幕以及缓存清空的功能 button_add(self) 实现两个数相加的功能 button_subtract(self) 实现两个数相减的功能 button_multiply(self) 实现两个数相乘的功能 button_divide(self) 实现两个数相除的功能 button_equal(self) 首先判断执行的运算操作调用对应的函数进行计算并将结果显示在文本框中
class Calculator(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle(计算器)self.setFixedSize(300, 350) # 固定窗口大小central_widget QWidget(self)self.setCentralWidget(central_widget)main_layout QGridLayout()central_widget.setLayout(main_layout)# 添加文本框self.screen QLineEdit()self.screen.setFixedHeight(40)self.screen.setAlignment(Qt.AlignRight)self.screen.setReadOnly(True)main_layout.addWidget(self.screen, 0, 0, 1, 4)# 添加按钮button_1 QPushButton(1, clickedlambda: self.button_click(1))button_2 QPushButton(2, clickedlambda: self.button_click(2))button_3 QPushButton(3, clickedlambda: self.button_click(3))button_4 QPushButton(4, clickedlambda: self.button_click(4))button_5 QPushButton(5, clickedlambda: self.button_click(5))button_6 QPushButton(6, clickedlambda: self.button_click(6))button_7 QPushButton(7, clickedlambda: self.button_click(7))button_8 QPushButton(8, clickedlambda: self.button_click(8))button_9 QPushButton(9, clickedlambda: self.button_click(9))button_0 QPushButton(0, clickedlambda: self.button_click(0))button_add QPushButton(, clickedself.button_add)button_subtract QPushButton(-, clickedself.button_subtract)button_multiply QPushButton(*, clickedself.button_multiply)button_divide QPushButton(/, clickedself.button_divide)button_clear QPushButton(清除, clickedself.button_clear)button_equal QPushButton(, clickedself.button_equal)main_layout.addWidget(button_7, 1, 0)main_layout.addWidget(button_8, 1, 1)main_layout.addWidget(button_9, 1, 2)main_layout.addWidget(button_divide, 1, 3)main_layout.addWidget(button_4, 2, 0)main_layout.addWidget(button_5, 2, 1)main_layout.addWidget(button_6, 2, 2)main_layout.addWidget(button_multiply, 2, 3)main_layout.addWidget(button_1, 3, 0)main_layout.addWidget(button_2, 3, 1)main_layout.addWidget(button_3, 3, 2)main_layout.addWidget(button_subtract, 3, 3)main_layout.addWidget(button_0, 4, 0)main_layout.addWidget(button_clear, 4, 1, 1, 2)main_layout.addWidget(button_add, 4, 3)main_layout.addWidget(button_equal, 5, 0, 1, 4)# 初始化变量self.first_num Noneself.operation Nonedef button_click(self, number):current self.screen.text()self.screen.setText(current number)def button_clear(self):self.screen.clear()self.first_num Noneself.operation Nonedef button_add(self):self.first_num float(self.screen.text())self.screen.clear()self.operation adddef button_subtract(self):self.first_num float(self.screen.text())self.screen.clear()self.operation subtractdef button_multiply(self):self.first_num float(self.screen.text())self.screen.clear()self.operation multiplydef button_divide(self):self.first_num float(self.screen.text())self.screen.clear()self.operation dividedef button_equal(self):second_num float(self.screen.text())self.screen.clear()if self.operation add:result self.first_num second_numelif self.operation subtract:result self.first_num - second_numelif self.operation multiply:result self.first_num * second_numelif self.operation divide:if second_num 0:result 除数不能为 0else:result self.first_num / second_numself.screen.setText(str(result))
三、主程序
主程序首先创建窗口对象接着创建计算器对象并显示窗口最后设定终止条件。
if __name__ __main__:app QApplication(sys.argv)calc Calculator()calc.show()sys.exit(app.exec_())
四、总结
这个计算器的优点
采用了PyQt5模块和Python语言进行设计实现较为简单易于理解和修改。可以进行基本运算操作包括加、减、乘、除等。
这个计算器的缺点
功能较为简单只能进行基础的数学运算无任何高级功能。输入输出仅支持数字和基本的加减乘除运算符号不支持其他字符、函数或变量。在除数为0时无法做出错误提示故看起来好像可以除以0一样。
综上所述该计算器适合作为一个小型的实验项目或界面设计入门示例但是并不够强大不能满足更多复杂应用场景的需求。