在这篇PyQt5文章中,我们将学习如何制作PyQt5 GUI计算器,在编码之前,我们先简单介绍一下PyQt5。

什么是PyQt5 ?

PyQt5是一个绑定Qt5 c++的GUI框架,用于c++编程语言,PyQt5用于编写各种GUI应用程序,从会计应用程序到科学家和工程师使用的可视化工具。

可以编写只有几十行长的PyQt5应用程序,1000到10000行的中型项目非常常见。PyQt5可以免费用于非商业目的使用,Qt和PyQt双重许可:这实质上允许他们被用来开发非商业应用程序必须反过来被许可使用一个可接受的开放源码许可如GNU通用公共许可证(GPL),或用于开发商业应用程序在这种情况下,一个商业PyQt执照和商业Qt必须购买许可证。

安装

可以使用pip命令安装PyQt5。

pip install pyqt5

现在这是我们文章的完整代码,有三个不同的Python文件,第一个是我们的设计,第二个是我们的逻辑文件,第三个是运行我们的main PyQt5应用程序。

这是设计文件,这个应用的设计文件是由Qt Designer创建的。

该文件是ui文件,但我已将该文件转换为Python文件,文件名称为ui_calculator.py。

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

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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Calculator(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(323, 398)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(20, 30, 281, 51))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setStyleSheet("QLabel {\n"
"\n"
"border:1px solid green;\n"
"background-color:white;\n"
"qproperty-alignment: \'AlignVCenter | AlignRight\';\n"
"\n"
"\n"
"\n"
"}")
        self.label.setObjectName("label")
        self.pushButton_clear = QtWidgets.QPushButton(Dialog)
        self.pushButton_clear.setGeometry(QtCore.QRect(20, 80, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_clear.setFont(font)
        self.pushButton_clear.setStyleSheet("QPushButton {\n"
"border:1px solid green;\n"
"\n"
"background-color:#2AFF55\n"
"\n"
"\n"
"}\n"
"\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#2AFFAA\n"
"\n"
"\n"
"\n"
"}")
        self.pushButton_clear.setObjectName("pushButton_clear")
        self.pushButton_divide = QtWidgets.QPushButton(Dialog)
        self.pushButton_divide.setGeometry(QtCore.QRect(230, 80, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_divide.setFont(font)
        self.pushButton_divide.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid gree;\n"
"background-color:#55FFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFFF;\n"
"\n"
"\n"
"}")
        self.pushButton_divide.setObjectName("pushButton_divide")
        self.pushButton_modulus = QtWidgets.QPushButton(Dialog)
        self.pushButton_modulus.setGeometry(QtCore.QRect(160, 80, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_modulus.setFont(font)
        self.pushButton_modulus.setStyleSheet("QPushButton {\n"
"border:1px solid green;\n"
"\n"
"background-color:#2AFF55\n"
"\n"
"\n"
"}\n"
"\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#2AFFAA\n"
"\n"
"\n"
"\n"
"}")
        self.pushButton_modulus.setObjectName("pushButton_modulus")
        self.pushButton_plusminus = QtWidgets.QPushButton(Dialog)
        self.pushButton_plusminus.setGeometry(QtCore.QRect(90, 80, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_plusminus.setFont(font)
        self.pushButton_plusminus.setStyleSheet("QPushButton {\n"
"border:1px solid green;\n"
"\n"
"background-color:#2AFF55\n"
"\n"
"\n"
"}\n"
"\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#2AFFAA\n"
"\n"
"\n"
"\n"
"}")
        self.pushButton_plusminus.setObjectName("pushButton_plusminus")
        self.pushButton_multiply = QtWidgets.QPushButton(Dialog)
        self.pushButton_multiply.setGeometry(QtCore.QRect(230, 140, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_multiply.setFont(font)
        self.pushButton_multiply.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid gree;\n"
"background-color:#55FFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFFF;\n"
"\n"
"\n"
"}")
        self.pushButton_multiply.setObjectName("pushButton_multiply")
        self.pushButton_nine = QtWidgets.QPushButton(Dialog)
        self.pushButton_nine.setGeometry(QtCore.QRect(160, 140, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_nine.setFont(font)
        self.pushButton_nine.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid green;\n"
"background-color:#AAFFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFD4\n"
"\n"
"\n"
"}")
        self.pushButton_nine.setObjectName("pushButton_nine")
        self.pushButton_eight = QtWidgets.QPushButton(Dialog)
        self.pushButton_eight.setGeometry(QtCore.QRect(90, 140, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_eight.setFont(font)
        self.pushButton_eight.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid green;\n"
"background-color:#AAFFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFD4\n"
"\n"
"\n"
"}")
        self.pushButton_eight.setObjectName("pushButton_eight")
        self.pushButton_seven = QtWidgets.QPushButton(Dialog)
        self.pushButton_seven.setGeometry(QtCore.QRect(20, 140, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_seven.setFont(font)
        self.pushButton_seven.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid green;\n"
"background-color:#AAFFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFD4\n"
"\n"
"\n"
"}")
        self.pushButton_seven.setObjectName("pushButton_seven")
        self.pushButton_minus = QtWidgets.QPushButton(Dialog)
        self.pushButton_minus.setGeometry(QtCore.QRect(230, 200, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_minus.setFont(font)
        self.pushButton_minus.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid gree;\n"
"background-color:#55FFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFFF;\n"
"\n"
"\n"
"}")
        self.pushButton_minus.setObjectName("pushButton_minus")
        self.pushButton_four = QtWidgets.QPushButton(Dialog)
        self.pushButton_four.setGeometry(QtCore.QRect(160, 200, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_four.setFont(font)
        self.pushButton_four.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid green;\n"
"background-color:#AAFFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFD4\n"
"\n"
"\n"
"}")
        self.pushButton_four.setObjectName("pushButton_four")
        self.pushButton_five = QtWidgets.QPushButton(Dialog)
        self.pushButton_five.setGeometry(QtCore.QRect(90, 200, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_five.setFont(font)
        self.pushButton_five.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid green;\n"
"background-color:#AAFFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFD4\n"
"\n"
"\n"
"}")
        self.pushButton_five.setObjectName("pushButton_five")
        self.pushButton_six = QtWidgets.QPushButton(Dialog)
        self.pushButton_six.setGeometry(QtCore.QRect(20, 200, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_six.setFont(font)
        self.pushButton_six.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid green;\n"
"background-color:#AAFFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFD4\n"
"\n"
"\n"
"}")
        self.pushButton_six.setObjectName("pushButton_six")
        self.pushButton_plus = QtWidgets.QPushButton(Dialog)
        self.pushButton_plus.setGeometry(QtCore.QRect(230, 260, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_plus.setFont(font)
        self.pushButton_plus.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid gree;\n"
"background-color:#55FFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFFF;\n"
"\n"
"\n"
"}")
        self.pushButton_plus.setObjectName("pushButton_plus")
        self.pushButton_one = QtWidgets.QPushButton(Dialog)
        self.pushButton_one.setGeometry(QtCore.QRect(160, 260, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_one.setFont(font)
        self.pushButton_one.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid green;\n"
"background-color:#AAFFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFD4\n"
"\n"
"\n"
"}")
        self.pushButton_one.setObjectName("pushButton_one")
        self.pushButton_two = QtWidgets.QPushButton(Dialog)
        self.pushButton_two.setGeometry(QtCore.QRect(90, 260, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_two.setFont(font)
        self.pushButton_two.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid green;\n"
"background-color:#AAFFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFD4\n"
"\n"
"\n"
"}")
        self.pushButton_two.setObjectName("pushButton_two")
        self.pushButton_three = QtWidgets.QPushButton(Dialog)
        self.pushButton_three.setGeometry(QtCore.QRect(20, 260, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_three.setFont(font)
        self.pushButton_three.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid green;\n"
"background-color:#AAFFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFD4\n"
"\n"
"\n"
"}")
        self.pushButton_three.setObjectName("pushButton_three")
        self.pushButton_zero = QtWidgets.QPushButton(Dialog)
        self.pushButton_zero.setGeometry(QtCore.QRect(20, 320, 141, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_zero.setFont(font)
        self.pushButton_zero.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid green;\n"
"background-color:#AAFFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFD4\n"
"\n"
"\n"
"}")
        self.pushButton_zero.setObjectName("pushButton_zero")
        self.pushButton_equal = QtWidgets.QPushButton(Dialog)
        self.pushButton_equal.setGeometry(QtCore.QRect(230, 320, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_equal.setFont(font)
        self.pushButton_equal.setStyleSheet("QPushButton {\n"
"\n"
"border:1px solid gree;\n"
"background-color:#55FFAA;\n"
"\n"
"\n"
"\n"
"}\n"
"\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#AAFFFF;\n"
"\n"
"\n"
"}")
        self.pushButton_equal.setObjectName("pushButton_equal")
        self.pushButton_decimel = QtWidgets.QPushButton(Dialog)
        self.pushButton_decimel.setGeometry(QtCore.QRect(160, 320, 71, 61))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_decimel.setFont(font)
        self.pushButton_decimel.setStyleSheet("QPushButton {\n"
"border:1px solid green;\n"
"\n"
"background-color:#2AFF55\n"
"\n"
"\n"
"}\n"
"\n"
"\n"
"QPushButton:pressed {\n"
"\n"
"background-color:#2AFFAA\n"
"\n"
"\n"
"\n"
"}")
        self.pushButton_decimel.setObjectName("pushButton_decimel")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "0"))
        self.pushButton_clear.setText(_translate("Dialog", "C"))
        self.pushButton_divide.setText(_translate("Dialog", "/"))
        self.pushButton_modulus.setText(_translate("Dialog", "%"))
        self.pushButton_plusminus.setText(_translate("Dialog", "+/-"))
        self.pushButton_multiply.setText(_translate("Dialog", "x"))
        self.pushButton_nine.setText(_translate("Dialog", "9"))
        self.pushButton_eight.setText(_translate("Dialog", "8"))
        self.pushButton_seven.setText(_translate("Dialog", "7"))
        self.pushButton_minus.setText(_translate("Dialog", "-"))
        self.pushButton_four.setText(_translate("Dialog", "4"))
        self.pushButton_five.setText(_translate("Dialog", "5"))
        self.pushButton_six.setText(_translate("Dialog", "6"))
        self.pushButton_plus.setText(_translate("Dialog", "+"))
        self.pushButton_one.setText(_translate("Dialog", "1"))
        self.pushButton_two.setText(_translate("Dialog", "2"))
        self.pushButton_three.setText(_translate("Dialog", "3"))
        self.pushButton_zero.setText(_translate("Dialog", "0"))
        self.pushButton_equal.setText(_translate("Dialog", "="))
        self.pushButton_decimel.setText(_translate("Dialog", "."))

这是我们的主逻辑文件,我将其称为calculator.py。

from PyQt5 import QtWidgets
from ui_calculator import Ui_Calculator

class calculatorWindow(QtWidgets.QMainWindow, Ui_Calculator):
    firNumber = None
    typingNumber = False

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.show()

        #our buttons
        self.pushButton_zero.clicked.connect(self.numberPressed)
        self.pushButton_one.clicked.connect(self.numberPressed)
        self.pushButton_two.clicked.connect(self.numberPressed)
        self.pushButton_three.clicked.connect(self.numberPressed)
        self.pushButton_four.clicked.connect(self.numberPressed)
        self.pushButton_five.clicked.connect(self.numberPressed)
        self.pushButton_six.clicked.connect(self.numberPressed)
        self.pushButton_seven.clicked.connect(self.numberPressed)
        self.pushButton_eight.clicked.connect(self.numberPressed)
        self.pushButton_nine.clicked.connect(self.numberPressed)

        self.pushButton_decimel.clicked.connect(self.decimalPressed)
        self.pushButton_plusminus.clicked.connect(self.unaryOperation)
        self.pushButton_modulus.clicked.connect(self.unaryOperation)

        self.pushButton_plus.clicked.connect(self.binaryOperation)
        self.pushButton_minus.clicked.connect(self.binaryOperation)
        self.pushButton_multiply.clicked.connect(self.binaryOperation)
        self.pushButton_divide.clicked.connect(self.binaryOperation)

        self.pushButton_equal.clicked.connect(self.equl_pressed)
        self.pushButton_clear.clicked.connect(self.clearbtn)

        self.pushButton_plus.setCheckable(True)
        self.pushButton_minus.setCheckable(True)
        self.pushButton_multiply.setCheckable(True)
        self.pushButton_divide.setCheckable(True)

    #number pressed method
    def numberPressed(self):
        button = self.sender()

        if ((
                        self.pushButton_plus.isChecked() or self.pushButton_minus.isChecked() or self.pushButton_divide.isChecked() or self.pushButton_multiply.isChecked()) and (
        not self.typingNumber)):
            lbl = format(float(button.text()), '.15g')
            self.typingNumber = True

        else:

            if (('.' in self.label.text()) and (button.text() == '0')):
                lbl = format(self.label.text() + button.text(), '.15')
            else:
                lbl = format(float(self.label.text() + button.text()), '.15g')

        self.label.setText(lbl)

    #decimel number pressed
    def decimalPressed(self):

         self.label.setText(self.label.text() + '.')

    #unaryopertio
    def unaryOperation(self):
        button = self.sender()

        labelNumber = float(self.label.text())

        if button.text() == "+/-":
            labelNumber = labelNumber * -1
        else:
            labelNumber = labelNumber * 0.01

        newLabel = format(labelNumber, '.15g')

        self.label.setText(newLabel)

    #binary operation
    def binaryOperation(self):
        button = self.sender()

        self.firNumber = float(self.label.text())

        button.setChecked(True)

    def equl_pressed(self):
        secondNumber = float(self.label.text())

        if(self.pushButton_plus.isChecked()):
            labelNumber = self.firNumber + secondNumber
            newLabel = format(labelNumber, '.15g')
            self.label.setText(newLabel)
            self.pushButton_plus.setChecked(False)

        elif (self.pushButton_minus.isChecked()):
            labelNumber = self.firNumber - secondNumber
            newLabel = format(labelNumber, '.15g')
            self.label.setText(newLabel)
            self.pushButton_minus.setChecked(False)

        elif (self.pushButton_multiply.isChecked()):
            labelNumber = self.firNumber * secondNumber
            newLabel = format(labelNumber, '.15g')
            self.label.setText(newLabel)
            self.pushButton_multiply.setChecked(False)

        elif (self.pushButton_divide.isChecked()):
            labelNumber = self.firNumber / secondNumber
            newLabel = format(labelNumber, '.15g')
            self.label.setText(newLabel)
            self.pushButton_divide.setChecked(False)
        self.typingNumber = False

    def clearbtn(self):
        self.pushButton_plus.setChecked(False)
        self.pushButton_minus.setChecked(False)
        self.pushButton_multiply.setChecked(False)
        self.pushButton_divide.setChecked(False)

        self.typingNumber = False

        self.label.setText("0")

这行代码用于导入ui文件。

from PyQt5 import QtWidgets
from ui_calculator import Ui_Calculator

我们已经创建了一个主类,它扩展了QMainWindow和UI_Calculator类。

您可以看到,我们在这个类中添加了两个变量,我们将使用它们。

firNumber = None
typingNumber = False

这些是我们的QPushButton,我们将其与特定的方法或槽连接。

self.pushButton_zero.clicked.connect(self.numberPressed)
        self.pushButton_one.clicked.connect(self.numberPressed)
        self.pushButton_two.clicked.connect(self.numberPressed)
        self.pushButton_three.clicked.connect(self.numberPressed)
        self.pushButton_four.clicked.connect(self.numberPressed)
        self.pushButton_five.clicked.connect(self.numberPressed)
        self.pushButton_six.clicked.connect(self.numberPressed)
        self.pushButton_seven.clicked.connect(self.numberPressed)
        self.pushButton_eight.clicked.connect(self.numberPressed)
        self.pushButton_nine.clicked.connect(self.numberPressed)

        self.pushButton_decimel.clicked.connect(self.decimalPressed)
        self.pushButton_plusminus.clicked.connect(self.unaryOperation)
        self.pushButton_modulus.clicked.connect(self.unaryOperation)

        self.pushButton_plus.clicked.connect(self.binaryOperation)
        self.pushButton_minus.clicked.connect(self.binaryOperation)
        self.pushButton_multiply.clicked.connect(self.binaryOperation)
        self.pushButton_divide.clicked.connect(self.binaryOperation)

        self.pushButton_equal.clicked.connect(self.equl_pressed)
        self.pushButton_clear.clicked.connect(self.clearbtn)

我们还将这些按钮连接到特定的方法或槽。

这是第三个文件,我把它命名为main。在这里,我们将运行我们的应用程序。

import sys
from PyQt5.QtWidgets import QApplication
from calculator import calculatorWindow

app = QApplication(sys.argv)
calculator = calculatorWindow()

sys.exit(app.exec())

因此,在这里每个PyQt5应用程序都必须创建一个应用程序对象。 sys.argv参数是命令行中的参数列表。

app = QApplication(sys.argv)

同样,我们进入了应用程序的主循环。 事件处理从这一点开始。 mainloop从窗口系统接收事件,并将其分配给应用程序小部件。 如果我们调用exit()方法或主窗口小部件被销毁,则主循环结束。

sys.exit(app.exec())

您需要从main.py文件运行代码,效果图如下:

发表评论