pyqt 实现计算器

1.由designer设计实现的计算器类

文件名为:untitled.py

# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(241, 341)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.lcdNumber = QtWidgets.QLCDNumber(self.centralwidget)self.lcdNumber.setGeometry(QtCore.QRect(30, 40, 151, 31))font = QtGui.QFont()font.setBold(False)font.setUnderline(False)font.setWeight(50)font.setStrikeOut(False)self.lcdNumber.setFont(font)self.lcdNumber.setDigitCount(10)self.lcdNumber.setMode(QtWidgets.QLCDNumber.Dec)self.lcdNumber.setObjectName("lcdNumber")self.pushButton1 = QtWidgets.QPushButton(self.centralwidget)self.pushButton1.setGeometry(QtCore.QRect(30, 110, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButton1.setFont(font)self.pushButton1.setObjectName("pushButton1")self.pushButton2 = QtWidgets.QPushButton(self.centralwidget)self.pushButton2.setGeometry(QtCore.QRect(70, 110, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButton2.setFont(font)self.pushButton2.setObjectName("pushButton2")self.pushButton3 = QtWidgets.QPushButton(self.centralwidget)self.pushButton3.setGeometry(QtCore.QRect(110, 110, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButton3.setFont(font)self.pushButton3.setObjectName("pushButton3")self.pushButton4 = QtWidgets.QPushButton(self.centralwidget)self.pushButton4.setGeometry(QtCore.QRect(30, 140, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButton4.setFont(font)self.pushButton4.setObjectName("pushButton4")self.pushButton8 = QtWidgets.QPushButton(self.centralwidget)self.pushButton8.setGeometry(QtCore.QRect(70, 170, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButton8.setFont(font)self.pushButton8.setObjectName("pushButton8")self.pushButton5 = QtWidgets.QPushButton(self.centralwidget)self.pushButton5.setGeometry(QtCore.QRect(70, 140, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButton5.setFont(font)self.pushButton5.setObjectName("pushButton5")self.pushButton6 = QtWidgets.QPushButton(self.centralwidget)self.pushButton6.setGeometry(QtCore.QRect(110, 140, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButton6.setFont(font)self.pushButton6.setObjectName("pushButton6")self.pushButton7 = QtWidgets.QPushButton(self.centralwidget)self.pushButton7.setGeometry(QtCore.QRect(30, 170, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButton7.setFont(font)self.pushButton7.setObjectName("pushButton7")self.pushButton9 = QtWidgets.QPushButton(self.centralwidget)self.pushButton9.setGeometry(QtCore.QRect(110, 170, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButton9.setFont(font)self.pushButton9.setObjectName("pushButton9")self.pushButtonJiafa = QtWidgets.QPushButton(self.centralwidget)self.pushButtonJiafa.setGeometry(QtCore.QRect(150, 110, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButtonJiafa.setFont(font)self.pushButtonJiafa.setObjectName("pushButtonJiafa")self.pushButtonJianf = QtWidgets.QPushButton(self.centralwidget)self.pushButtonJianf.setGeometry(QtCore.QRect(150, 140, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButtonJianf.setFont(font)self.pushButtonJianf.setObjectName("pushButtonJianf")self.pushButtonChuaf = QtWidgets.QPushButton(self.centralwidget)self.pushButtonChuaf.setGeometry(QtCore.QRect(150, 200, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButtonChuaf.setFont(font)self.pushButtonChuaf.setObjectName("pushButtonChuaf")self.pushButtonX = QtWidgets.QPushButton(self.centralwidget)self.pushButtonX.setGeometry(QtCore.QRect(150, 170, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButtonX.setFont(font)self.pushButtonX.setObjectName("pushButtonX")self.pushButtonAnswer = QtWidgets.QPushButton(self.centralwidget)self.pushButtonAnswer.setGeometry(QtCore.QRect(110, 200, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButtonAnswer.setFont(font)self.pushButtonAnswer.setObjectName("pushButtonAnswer")self.pushButton0 = QtWidgets.QPushButton(self.centralwidget)self.pushButton0.setGeometry(QtCore.QRect(70, 200, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButton0.setFont(font)self.pushButton0.setObjectName("pushButton0")self.pushButtonClear = QtWidgets.QPushButton(self.centralwidget)self.pushButtonClear.setGeometry(QtCore.QRect(30, 200, 31, 21))font = QtGui.QFont()font.setBold(True)font.setUnderline(False)font.setWeight(75)self.pushButtonClear.setFont(font)self.pushButtonClear.setObjectName("pushButtonClear")self.line = QtWidgets.QFrame(self.centralwidget)self.line.setGeometry(QtCore.QRect(20, 240, 171, 16))self.line.setFrameShape(QtWidgets.QFrame.HLine)self.line.setFrameShadow(QtWidgets.QFrame.Sunken)self.line.setObjectName("line")self.line_2 = QtWidgets.QFrame(self.centralwidget)self.line_2.setGeometry(QtCore.QRect(20, 20, 171, 16))self.line_2.setFrameShape(QtWidgets.QFrame.HLine)self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)self.line_2.setObjectName("line_2")self.line_3 = QtWidgets.QFrame(self.centralwidget)self.line_3.setGeometry(QtCore.QRect(10, 30, 20, 221))self.line_3.setFrameShape(QtWidgets.QFrame.VLine)self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)self.line_3.setObjectName("line_3")self.line_4 = QtWidgets.QFrame(self.centralwidget)self.line_4.setGeometry(QtCore.QRect(180, 30, 20, 221))self.line_4.setFrameShape(QtWidgets.QFrame.VLine)self.line_4.setFrameShadow(QtWidgets.QFrame.Sunken)self.line_4.setObjectName("line_4")MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 241, 22))self.menubar.setObjectName("menubar")MainWindow.setMenuBar(self.menubar)self.statusbar = QtWidgets.QStatusBar(MainWindow)self.statusbar.setObjectName("statusbar")MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.pushButton1.setText(_translate("MainWindow", "1"))self.pushButton2.setText(_translate("MainWindow", "2"))self.pushButton3.setText(_translate("MainWindow", "3"))self.pushButton4.setText(_translate("MainWindow", "4"))self.pushButton8.setText(_translate("MainWindow", "8"))self.pushButton5.setText(_translate("MainWindow", "5"))self.pushButton6.setText(_translate("MainWindow", "6"))self.pushButton7.setText(_translate("MainWindow", "7"))self.pushButton9.setText(_translate("MainWindow", "9"))self.pushButtonJiafa.setText(_translate("MainWindow", "+"))self.pushButtonJianf.setText(_translate("MainWindow", "-"))self.pushButtonChuaf.setText(_translate("MainWindow", "/"))self.pushButtonX.setText(_translate("MainWindow", "x"))self.pushButtonAnswer.setText(_translate("MainWindow", "="))self.pushButton0.setText(_translate("MainWindow", "0"))self.pushButtonClear.setText(_translate("MainWindow", "c"))

2.实现计算器类的实例化,窗口展示并添加逻辑功能

import sys
from untitled import Ui_MainWindow #引入在designer里编辑的类
from PyQt5.QtCore import *         #包含了包的核心的非gui功能
from PyQt5.QtGui import *from PyQt5.QtWidgets import *      #包含了ui元素组件#实体化designer里编辑的类,然后在继承
class calculator(QMainWindow, Ui_MainWindow):def __init__(self):super().__init__()self.num_cal = 0self.setupUi(self)self.num1 = 0self.num2 = 0#触发器的固定格式@pyqtSlot()def on_pushButton1_clicked(self):self.lcdNumber.display(int(self.lcdNumber.value()*10+1))# print(self.lcdNumber.value())@pyqtSlot()def on_pushButton2_clicked(self):self.lcdNumber.display(int(self.lcdNumber.value()*10+2))@pyqtSlot()def on_pushButton3_clicked(self):self.lcdNumber.display(int(self.lcdNumber.value()*10+3))@pyqtSlot()def on_pushButton4_clicked(self):self.lcdNumber.display(int(self.lcdNumber.value()*10+4))@pyqtSlot()def on_pushButton5_clicked(self):self.lcdNumber.display(int(self.lcdNumber.value()*10+5))@pyqtSlot()def on_pushButton6_clicked(self):self.lcdNumber.display(int(self.lcdNumber.value()*10+6))@pyqtSlot()def on_pushButton7_clicked(self):self.lcdNumber.display(int(self.lcdNumber.value()*10+7))@pyqtSlot()def on_pushButton8_clicked(self):self.lcdNumber.display(int(self.lcdNumber.value()*10+8))@pyqtSlot()def on_pushButton9_clicked(self):self.lcdNumber.display(int(self.lcdNumber.value()*10+9))@pyqtSlot()def on_pushButton0_clicked(self):self.lcdNumber.display(int(self.lcdNumber.value()*10+0))@pyqtSlot()def on_pushButtonJiafa_clicked(self):if(self.num_cal == 0):self.num1 = self.lcdNumber.value()# print(num1)# else:#     self.num2 = self.lcdNumber.value()# print(num2)self.num_cal = '+'print(self.num_cal)self.lcdNumber.display(0)@pyqtSlot()def on_pushButtonJianf_clicked(self):self.num1 = self.lcdNumber.value()self.num_cal = '-'self.lcdNumber.display(0)@pyqtSlot()def on_pushButtonX_clicked(self):self.num1 = self.lcdNumber.value()self.num_cal = '*'self.lcdNumber.display(0)@pyqtSlot()def on_pushButtonChuaf_clicked(self):self.num1 = self.lcdNumber.value()self.num_cal = '/'self.lcdNumber.display(0)@pyqtSlot()def on_pushButtonClear_clicked(self):self.lcdNumber.display(0)@pyqtSlot()def on_pushButtonAnswer_clicked(self):self.num2 = self.lcdNumber.value()if self.num_cal == '+':ans = self.num1 + self.num2print(ans)print(self.num2)self.lcdNumber.display(ans)elif self.num_cal == '-':ans = self.num1 - self.num2print(ans)print(self.num2)self.lcdNumber.display(ans)elif self.num_cal == '*':ans = self.num1 * self.num2print(ans)print(self.num2)self.lcdNumber.display(ans)elif self.num_cal == '/':ans = self.num1 / self.num2print(ans)print(self.num2)self.lcdNumber.display(ans)if __name__ == '__main__':app = QApplication(sys.argv)ui = calculator()ui.show()sys.exit(app.exec_())

3.效果展示

4.总结:

QApplication:

QApplication 类管理图形用户界面应用程序的控制流和主要设置。 可以说 QApplication是Qt的整个后台管理的命脉

app = QApplication(sys.argv)这句作用用来初始化窗口系统 

app.exec():

app.exec_()的作用是运行主循环,必须调用此函数才能开始事件处理,调用该方法进入程序的主循环直到调用exit()结束。主事件循环从窗口系统接收事件,并将其分派给应用程序小部件。如果没有该方法,那么在运行的时候还没有进入程序的主循环就直接结束了,所以运行的时候窗口会闪退。
app.exec_()在退出时会返回状态代码

sys.exit(n): 

不用sys.exit(app.exec_()),只使用app.exec_(),程序也可以正常运行,但是关闭窗口后进程却不会退出。

sys.exit(n)的作用是退出应用程序并返回n到父进程。

 参考文章:


原文链接:https://blog.csdn.net/cuicui_ruirui/article/details/102873528

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/10.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

03-MySQL-基础篇-SQL之DDL语句

SQL之DDL语句 前言DDL数据库操作表操作查询操作数据类型案例修改删除 前言 本篇来学习下SQL中的DDL语句 DDL 全称Data Definition Language,数据定义语言,用来定义数据库对象(数据库,表,字段) 数据库操作 查询所有数据库 sh…

docker 发布镜像到阿里云容器镜像

1.登录阿里云 2.创建个人版实例,这里需要设置密码,一定要保存好 3.创建一个镜像仓库,个人实例最多创建3个 4.创建命名空间,个人实例最多300个 5.发布镜像到阿里云容器镜像 1、linux登录阿里云docker容器镜像 $ docker login --us…

考场作弊行为自动抓拍告警算法 yolov7

考场作弊行为自动抓拍告警系统通过yolov7python网络模型算法,考场作弊行为自动抓拍告警算法实时监测考场内所有考生的行为,对考生的行为进行自动抓拍,并分析判断是否存在作弊行为。YOLOv7 的发展方向与当前主流的实时目标检测器不同&#xff…

Clion 配置Mingw64的 c++开发环境

1、Mingw64的安装与环境变量的配置 Mingw64文件下载 Mingw64下载地址:https://sourceforge.net/projects/mingw-w64/files/ posix相比win32拥有C 11多线程特性,sjlj和seh对应异常处理特性,sjlj较为古老,所以选择seh 配置环境变…

Windows git bash输入vim报错,不能使用vim-plug插件管理器

Windows系统下的git bash在安装时自带了默认的vim&#xff0c;我自己也下了个gvim&#xff0c;并且配置了.vimrc&#xff0c;其中使用了vim-plug管理nerdtree这些插件。但是在bash中vim <file>时&#xff0c;就会蹦出来几行报错&#xff1a; 处理 /c/Users/<username…

训练自己的ChatGPT 语言模型(一).md

0x00 Background 为什么研究这个&#xff1f; ChatGPT在国内外都受到了广泛关注&#xff0c;很多高校、研究机构和企业都计划推出类似的模型。然而&#xff0c;ChatGPT并没有开源&#xff0c;且复现难度非常大&#xff0c;即使到现在&#xff0c;没有任何单位或企业能够完全复…