学习使用PySide2制作一个简单的计算器

使用工具

  • python + PySide2

实现思路

  • 利用python脚本执行函数eval(expression),求解算式字符串
  • 利用PySide2实现界面

最终效果

学习使用PySide2制作一个简单的计算器

代码:

ui_calculator.py

from PySide2 import QtCore, QtGui, QtWidgets

class Ui_Calculator(object):
    def setupUi(self, Calculator):
        Calculator.setObjectName("Calculator")
        Calculator.resize(480, 430)
        Calculator.setMinimumSize(QtCore.QSize(480, 430))
        Calculator.setMaximumSize(QtCore.QSize(480, 430))
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(Calculator)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setSpacing(10)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(Calculator)
        self.label.setMinimumSize(QtCore.QSize(0, 80))
        font = QtGui.QFont()
        font.setFamily("黑体")
        font.setPointSize(15)
        self.label.setFont(font)
        self.label.setStyleSheet("background-color: rgb(255, 255, 255);\n"
"")
        self.label.setFrameShape(QtWidgets.QFrame.Box)
        self.label.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.label.setWordWrap(True)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setSpacing(30)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setSpacing(12)
        self.gridLayout.setObjectName("gridLayout")
        self.btnR = QtWidgets.QPushButton(Calculator)
        self.btnR.setMinimumSize(QtCore.QSize(60, 60))
        self.btnR.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnR.setFont(font)
        self.btnR.setObjectName("btnR")
        self.gridLayout.addWidget(self.btnR, 0, 1, 1, 1)
        self.btn4 = QtWidgets.QPushButton(Calculator)
        self.btn4.setMinimumSize(QtCore.QSize(60, 60))
        self.btn4.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btn4.setFont(font)
        self.btn4.setObjectName("btn4")
        self.gridLayout.addWidget(self.btn4, 2, 0, 1, 1)
        self.btn7 = QtWidgets.QPushButton(Calculator)
        self.btn7.setMinimumSize(QtCore.QSize(60, 60))
        self.btn7.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btn7.setFont(font)
        self.btn7.setObjectName("btn7")
        self.gridLayout.addWidget(self.btn7, 1, 0, 1, 1)
        self.btn8 = QtWidgets.QPushButton(Calculator)
        self.btn8.setMinimumSize(QtCore.QSize(60, 60))
        self.btn8.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btn8.setFont(font)
        self.btn8.setObjectName("btn8")
        self.gridLayout.addWidget(self.btn8, 1, 1, 1, 1)
        self.btnL = QtWidgets.QPushButton(Calculator)
        self.btnL.setMinimumSize(QtCore.QSize(60, 60))
        self.btnL.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnL.setFont(font)
        self.btnL.setObjectName("btnL")
        self.gridLayout.addWidget(self.btnL, 0, 0, 1, 1)
        self.btn9 = QtWidgets.QPushButton(Calculator)
        self.btn9.setMinimumSize(QtCore.QSize(60, 60))
        self.btn9.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btn9.setFont(font)
        self.btn9.setObjectName("btn9")
        self.gridLayout.addWidget(self.btn9, 1, 2, 1, 1)
        self.btnDel = QtWidgets.QPushButton(Calculator)
        self.btnDel.setMinimumSize(QtCore.QSize(60, 60))
        self.btnDel.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnDel.setFont(font)
        self.btnDel.setObjectName("btnDel")
        self.gridLayout.addWidget(self.btnDel, 0, 2, 1, 1)
        self.btn5 = QtWidgets.QPushButton(Calculator)
        self.btn5.setMinimumSize(QtCore.QSize(60, 60))
        self.btn5.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btn5.setFont(font)
        self.btn5.setObjectName("btn5")
        self.gridLayout.addWidget(self.btn5, 2, 1, 1, 1)
        self.btn6 = QtWidgets.QPushButton(Calculator)
        self.btn6.setMinimumSize(QtCore.QSize(60, 60))
        self.btn6.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btn6.setFont(font)
        self.btn6.setObjectName("btn6")
        self.gridLayout.addWidget(self.btn6, 2, 2, 1, 1)
        self.btn1 = QtWidgets.QPushButton(Calculator)
        self.btn1.setMinimumSize(QtCore.QSize(60, 60))
        self.btn1.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btn1.setFont(font)
        self.btn1.setObjectName("btn1")
        self.gridLayout.addWidget(self.btn1, 3, 0, 1, 1)
        self.btn2 = QtWidgets.QPushButton(Calculator)
        self.btn2.setMinimumSize(QtCore.QSize(60, 60))
        self.btn2.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btn2.setFont(font)
        self.btn2.setObjectName("btn2")
        self.gridLayout.addWidget(self.btn2, 3, 1, 1, 1)
        self.btn3 = QtWidgets.QPushButton(Calculator)
        self.btn3.setMinimumSize(QtCore.QSize(60, 60))
        self.btn3.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btn3.setFont(font)
        self.btn3.setObjectName("btn3")
        self.gridLayout.addWidget(self.btn3, 3, 2, 1, 1)
        self.btn0 = QtWidgets.QPushButton(Calculator)
        self.btn0.setMinimumSize(QtCore.QSize(60, 60))
        self.btn0.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btn0.setFont(font)
        self.btn0.setObjectName("btn0")
        self.gridLayout.addWidget(self.btn0, 4, 0, 1, 1)
        self.btnDot = QtWidgets.QPushButton(Calculator)
        self.btnDot.setMinimumSize(QtCore.QSize(60, 60))
        self.btnDot.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnDot.setFont(font)
        self.btnDot.setObjectName("btnDot")
        self.gridLayout.addWidget(self.btnDot, 4, 1, 1, 1)
        self.btnNegative = QtWidgets.QPushButton(Calculator)
        self.btnNegative.setMinimumSize(QtCore.QSize(60, 60))
        self.btnNegative.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnNegative.setFont(font)
        self.btnNegative.setObjectName("btnNegative")
        self.gridLayout.addWidget(self.btnNegative, 4, 2, 1, 1)
        self.horizontalLayout.addLayout(self.gridLayout)
        self.gridLayout_2 = QtWidgets.QGridLayout()
        self.gridLayout_2.setSpacing(12)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.btnClear = QtWidgets.QPushButton(Calculator)
        self.btnClear.setMinimumSize(QtCore.QSize(120, 60))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnClear.setFont(font)
        self.btnClear.setObjectName("btnClear")
        self.gridLayout_2.addWidget(self.btnClear, 0, 0, 1, 2)
        self.btnPlus = QtWidgets.QPushButton(Calculator)
        self.btnPlus.setMinimumSize(QtCore.QSize(60, 60))
        self.btnPlus.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnPlus.setFont(font)
        self.btnPlus.setObjectName("btnPlus")
        self.gridLayout_2.addWidget(self.btnPlus, 1, 0, 1, 1)
        self.btnMinus = QtWidgets.QPushButton(Calculator)
        self.btnMinus.setMinimumSize(QtCore.QSize(60, 60))
        self.btnMinus.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnMinus.setFont(font)
        self.btnMinus.setObjectName("btnMinus")
        self.gridLayout_2.addWidget(self.btnMinus, 1, 1, 1, 1)
        self.btnMulti = QtWidgets.QPushButton(Calculator)
        self.btnMulti.setMinimumSize(QtCore.QSize(60, 60))
        self.btnMulti.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnMulti.setFont(font)
        self.btnMulti.setObjectName("btnMulti")
        self.gridLayout_2.addWidget(self.btnMulti, 2, 0, 1, 1)
        self.btnDivide = QtWidgets.QPushButton(Calculator)
        self.btnDivide.setMinimumSize(QtCore.QSize(60, 60))
        self.btnDivide.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnDivide.setFont(font)
        self.btnDivide.setObjectName("btnDivide")
        self.gridLayout_2.addWidget(self.btnDivide, 2, 1, 1, 1)
        self.btnMod = QtWidgets.QPushButton(Calculator)
        self.btnMod.setMinimumSize(QtCore.QSize(60, 60))
        self.btnMod.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnMod.setFont(font)
        self.btnMod.setObjectName("btnMod")
        self.gridLayout_2.addWidget(self.btnMod, 3, 0, 1, 1)
        self.btnSqrt = QtWidgets.QPushButton(Calculator)
        self.btnSqrt.setMinimumSize(QtCore.QSize(60, 60))
        self.btnSqrt.setMaximumSize(QtCore.QSize(90, 90))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnSqrt.setFont(font)
        self.btnSqrt.setObjectName("btnSqrt")
        self.gridLayout_2.addWidget(self.btnSqrt, 3, 1, 1, 1)
        self.btnEqual = QtWidgets.QPushButton(Calculator)
        self.btnEqual.setMinimumSize(QtCore.QSize(120, 60))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.btnEqual.setFont(font)
        self.btnEqual.setObjectName("btnEqual")
        self.gridLayout_2.addWidget(self.btnEqual, 4, 0, 1, 2)
        self.horizontalLayout.addLayout(self.gridLayout_2)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_2.addLayout(self.verticalLayout)

        self.retranslateUi(Calculator)
        QtCore.QMetaObject.connectSlotsByName(Calculator)

    def retranslateUi(self, Calculator):
        Calculator.setWindowTitle(QtWidgets.QApplication.translate("Calculator", "Dialog", None, -1))
        self.label.setText(QtWidgets.QApplication.translate("Calculator", "TextLabel", None, -1))
        self.btnR.setText(QtWidgets.QApplication.translate("Calculator", ")", None, -1))
        self.btn4.setText(QtWidgets.QApplication.translate("Calculator", "4", None, -1))
        self.btn7.setText(QtWidgets.QApplication.translate("Calculator", "7", None, -1))
        self.btn8.setText(QtWidgets.QApplication.translate("Calculator", "8", None, -1))
        self.btnL.setText(QtWidgets.QApplication.translate("Calculator", "(", None, -1))
        self.btn9.setText(QtWidgets.QApplication.translate("Calculator", "9", None, -1))
        self.btnDel.setText(QtWidgets.QApplication.translate("Calculator", "<-", None, -1))
        self.btn5.setText(QtWidgets.QApplication.translate("Calculator", "5", None, -1))
        self.btn6.setText(QtWidgets.QApplication.translate("Calculator", "6", None, -1))
        self.btn1.setText(QtWidgets.QApplication.translate("Calculator", "1", None, -1))
        self.btn2.setText(QtWidgets.QApplication.translate("Calculator", "2", None, -1))
        self.btn3.setText(QtWidgets.QApplication.translate("Calculator", "3", None, -1))
        self.btn0.setText(QtWidgets.QApplication.translate("Calculator", "0", None, -1))
        self.btnDot.setText(QtWidgets.QApplication.translate("Calculator", ".", None, -1))
        self.btnNegative.setText(QtWidgets.QApplication.translate("Calculator", "-/+", None, -1))
        self.btnClear.setText(QtWidgets.QApplication.translate("Calculator", "C", None, -1))
        self.btnPlus.setText(QtWidgets.QApplication.translate("Calculator", "+", None, -1))
        self.btnMinus.setText(QtWidgets.QApplication.translate("Calculator", "-", None, -1))
        self.btnMulti.setText(QtWidgets.QApplication.translate("Calculator", "*", None, -1))
        self.btnDivide.setText(QtWidgets.QApplication.translate("Calculator", "/", None, -1))
        self.btnMod.setText(QtWidgets.QApplication.translate("Calculator", "%", None, -1))
        self.btnSqrt.setText(QtWidgets.QApplication.translate("Calculator", "sqrt", None, -1))
        self.btnEqual.setText(QtWidgets.QApplication.translate("Calculator", "=", None, -1))

calculator.py

import sys
from math import sqrt
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from ui_calculator import Ui_Calculator

class Window(QWidget,Ui_Calculator):
    def __init__(self,parent = None):
        super().__init__()
        self.setupUi(self)

        # 需要用脚本求解的算式字符串
        self.expression = str()
        self.label.setText(self.expression)

        # 获取ui文件中的QPushButton对象
        btnList = self.findChildren(QPushButton)

        # 信号槽连接
        for btn in btnList:
            btn.clicked.connect(self.slotFunction)


    @Slot()
    def slotFunction(self):
        dict = {self.btn0:'0',self.btn1:'1',self.btn2:'2',self.btn3:'3',
                self.btn4:'4',self.btn5:'5',self.btn6:'6',self.btn7:'7',
                self.btn8:'8',self.btn9:'9',self.btnL:'(',self.btnR:')',
                self.btnDot:'.',self.btnNegative:'-',self.btnPlus:'+',
                self.btnMinus:'-',self.btnMulti:'*',self.btnDivide:'/',
                self.btnSqrt:'sqrt',self.btnMod:'%'
                }

        if self.sender() in dict.keys():
            value = dict.get(self.sender())
            if '=' not in self.expression:
                self.expression += value

        elif self.sender() is self.btnDel:      # '<-'按键
            # 判断算式是否为空
            if len(self.expression):
                # 判断是否算出答案,是则算式中含=,否则不含=
                if '=' not in self.expression:
                    self.expression = self.expression[:-1]  # 删除末尾一个字符
                else:
                    self.expression = str()     # 删除整个算式
        elif self.sender() is self.btnClear:    # 'C'按键
            self.expression = str()
        elif self.sender() is self.btnEqual:    # '='按键
            if '=' not in self.expression and len(self.expression):
                try:
                    result = eval(self.expression)
                    self.expression += ('='+str(result))
                except:
                    self.expression += '=Error'
            else:
                pass

        self.label.setText(self.expression)




if __name__ == '__main__':
    app =QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())


学习练手项目,若有不足,欢迎批评指正。

上一篇:python实现端口扫描器


下一篇:按下葫芦起了瓢