Python基于Qt平台的计算器源代码

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow(object):

    content = ''
    symbol = ['+', '-', '×', '÷', '%', '.', '=']

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("Calculator")
        MainWindow.resize(360, 460)

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.centralwidget.setStyleSheet('''
        QPushButton{
        font-size: 15px;
        background: rgba(255, 255, 255, 0.7);
        border-radius:5px;
        }
        QTextBrowser{
        font-size: 25px;
        background: rgba(255, 255, 255, 0.7);
        border-radius:10px;
        text-align:right;
        margin-right:0px;
        }
        ''')

        self.btn1 = QtWidgets.QPushButton(self.centralwidget)
        self.btn1.setGeometry(QtCore.QRect(50, 180, 51, 51))
        self.btn1.setObjectName("btn1")

        self.btn2 = QtWidgets.QPushButton(self.centralwidget)
        self.btn2.setGeometry(QtCore.QRect(120, 180, 51, 51))
        self.btn2.setObjectName("btn2")

        self.btn3 = QtWidgets.QPushButton(self.centralwidget)
        self.btn3.setGeometry(QtCore.QRect(190, 180, 51, 51))
        self.btn3.setObjectName("btn3")

        self.btn4 = QtWidgets.QPushButton(self.centralwidget)
        self.btn4.setGeometry(QtCore.QRect(50, 240, 51, 51))
        self.btn4.setObjectName("btn4")

        self.btn5 = QtWidgets.QPushButton(self.centralwidget)
        self.btn5.setGeometry(QtCore.QRect(120, 240, 51, 51))
        self.btn5.setObjectName("btn5")

        self.btn6 = QtWidgets.QPushButton(self.centralwidget)
        self.btn6.setGeometry(QtCore.QRect(190, 240, 51, 51))
        self.btn6.setObjectName("btn6")

        self.btn7 = QtWidgets.QPushButton(self.centralwidget)
        self.btn7.setGeometry(QtCore.QRect(50, 300, 51, 51))
        self.btn7.setObjectName("btn7")

        self.btn8 = QtWidgets.QPushButton(self.centralwidget)
        self.btn8.setGeometry(QtCore.QRect(120, 300, 51, 51))
        self.btn8.setObjectName("btn8")

        self.btn9 = QtWidgets.QPushButton(self.centralwidget)
        self.btn9.setGeometry(QtCore.QRect(190, 300, 51, 51))
        self.btn9.setObjectName("btn9")

        self.btn0 = QtWidgets.QPushButton(self.centralwidget)
        self.btn0.setGeometry(QtCore.QRect(50, 360, 51, 51))
        self.btn0.setObjectName("btn0")

        self.btn_double = QtWidgets.QPushButton(self.centralwidget)
        self.btn_double.setGeometry(QtCore.QRect(120, 360, 51, 51))
        self.btn_double.setObjectName("btn_double")

        self.btn_mult = QtWidgets.QPushButton(self.centralwidget)
        self.btn_mult.setGeometry(QtCore.QRect(260, 180, 51, 51))
        self.btn_mult.setObjectName("btn_mult")

        self.btn_sub = QtWidgets.QPushButton(self.centralwidget)
        self.btn_sub.setGeometry(QtCore.QRect(260, 240, 51, 51))
        self.btn_sub.setObjectName("btn_sub")

        self.btn_equal = QtWidgets.QPushButton(self.centralwidget)
        self.btn_equal.setGeometry(QtCore.QRect(190, 360, 121, 51))
        self.btn_equal.setObjectName("btn_equal")

        self.btn_add = QtWidgets.QPushButton(self.centralwidget)
        self.btn_add.setGeometry(QtCore.QRect(260, 300, 51, 51))
        self.btn_add.setObjectName("btn_add")

        self.btn_back = QtWidgets.QPushButton(self.centralwidget)
        self.btn_back.setGeometry(QtCore.QRect(50, 120, 51, 51))
        self.btn_back.setObjectName("btn_back")

        self.btn_clear = QtWidgets.QPushButton(self.centralwidget)
        self.btn_clear.setGeometry(QtCore.QRect(120, 120, 51, 51))
        self.btn_clear.setObjectName("btn_clear")

        self.btn_percent = QtWidgets.QPushButton(self.centralwidget)
        self.btn_percent.setGeometry(QtCore.QRect(190, 120, 51, 51))
        self.btn_percent.setObjectName("btn_percent")

        self.btn_division = QtWidgets.QPushButton(self.centralwidget)
        self.btn_division.setGeometry(QtCore.QRect(260, 120, 51, 51))
        self.btn_division.setObjectName("btn_division")

        self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setLayoutDirection(QtCore.Qt.RightToLeft)
        self.textBrowser.setGeometry(QtCore.QRect(50, 50, 261, 51))
        self.textBrowser.setObjectName("textBrowser")

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 727, 23))
        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)
        self.btn0.clicked.connect(lambda: self.write(0))
        self.btn1.clicked.connect(lambda: self.write(1))
        self.btn2.clicked.connect(lambda: self.write(2))
        self.btn3.clicked.connect(lambda: self.write(3))
        self.btn4.clicked.connect(lambda: self.write(4))
        self.btn5.clicked.connect(lambda: self.write(5))
        self.btn6.clicked.connect(lambda: self.write(6))
        self.btn7.clicked.connect(lambda: self.write(7))
        self.btn8.clicked.connect(lambda: self.write(8))
        self.btn9.clicked.connect(lambda: self.write(9))
        self.btn_double.clicked.connect(lambda: self.write('.'))
        self.btn_mult.clicked.connect(lambda: self.write('×'))
        self.btn_sub.clicked.connect(lambda: self.write('-'))
        self.btn_equal.clicked.connect(self.calculater)
        self.btn_add.clicked.connect(lambda: self.write('+'))
        self.btn_back.clicked.connect(self.back)
        self.btn_clear.clicked.connect(self.clear)
        self.btn_percent.clicked.connect(lambda: self.write('%'))
        self.btn_division.clicked.connect(lambda: self.write('÷'))

    # 写入
    def write(self, param):
        # 如果上一次和这一次输入的都是符号,则替换
        if len(self.content) > 0 and (self.content[-1] in self.symbol) and (param in self.symbol):
            self.content = self.content[0:len(self.content) - 1] + str(param)
        # 排除第一位输入符号
        elif len(self.content) == 0 and (param in self.symbol):
            pass
        else:
            self.content = self.content + str(param)
        self.textBrowser.setText(self.content)

    # 清空
    def clear(self):
        self.content = ""
        self.textBrowser.setText("")

    # 后退一位
    def back(self):
        self.content = self.content[0:len(self.content) - 1]
        self.textBrowser.setText(self.content)

    # 计算
    def calculater(self):
        # 判断最后一位是不是运算符
        if len(self.content) > 0 and (self.content[-1] in self.symbol):
            pass
        else:
            new_str = self.content.replace('×', '*')
            new_str = new_str.replace('÷', '/')
            self.content = self.content + '=' + str(eval(new_str))
            self.textBrowser.setText(self.content)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.btn1.setText(_translate("MainWindow", "1"))
        self.btn2.setText(_translate("MainWindow", "2"))
        self.btn3.setText(_translate("MainWindow", "3"))
        self.btn4.setText(_translate("MainWindow", "4"))
        self.btn5.setText(_translate("MainWindow", "5"))
        self.btn6.setText(_translate("MainWindow", "6"))
        self.btn7.setText(_translate("MainWindow", "7"))
        self.btn8.setText(_translate("MainWindow", "8"))
        self.btn9.setText(_translate("MainWindow", "9"))
        self.btn0.setText(_translate("MainWindow", "0"))
        self.btn_double.setText(_translate("MainWindow", "."))
        self.btn_mult.setText(_translate("MainWindow", "×"))
        self.btn_sub.setText(_translate("MainWindow", "-"))
        self.btn_equal.setText(_translate("MainWindow", "="))
        self.btn_add.setText(_translate("MainWindow", "+"))
        self.btn_back.setText(_translate("MainWindow", "AC"))
        self.btn_clear.setText(_translate("MainWindow", "Clear"))
        self.btn_percent.setText(_translate("MainWindow", "%"))
        self.btn_division.setText(_translate("MainWindow", "÷"))

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
原文链接:,转发请注明来源!
评论已关闭。