计算器最终的效果图:
一、界面布局:
QtDesiger设计中的objectName从左至右,从上之下依次为:
lineEdit
pushButton_zuo, pushButton_you, pushButton_clear, pushButton_back
pushButton_777, pushButton_888, pushButton_999 , pushButton_chu
pushButton_444, pushButton_555, pushButton_666 , pushButton_cheng
pushButton_111, pushButton_222, pushButton_333 , pushButton_jian
pushButton_000, pushButton_dian,pushButton_dengyu,pushButton_jia
针对lineEdit的图例:
针对左括号的图例:
需要注意的是回退键←,这个符号比较特殊,键盘上没有对应的键,需要打开输入法从特殊符号中获取,如果不知道怎样取打出这个特殊符号,就从文中直接复制即可。
至此界面完成。
二、逻辑处理
1、所有界面的button与计算函数绑定
(20个pushbutton均绑定到on_calculate函数中),绑定方法为button.clicked.connect(self.on_calculate)
20个按钮的绑定代码如下:
self.pushButton_000.clicked.connect(self.on_calculate)
self.pushButton_111.clicked.connect(self.on_calculate)
self.pushButton_222.clicked.connect(self.on_calculate)
self.pushButton_333.clicked.connect(self.on_calculate)
self.pushButton_444.clicked.connect(self.on_calculate)
self.pushButton_555.clicked.connect(self.on_calculate)
self.pushButton_666.clicked.connect(self.on_calculate)
self.pushButton_777.clicked.connect(self.on_calculate)
self.pushButton_888.clicked.connect(self.on_calculate)
self.pushButton_999.clicked.connect(self.on_calculate)
self.pushButton_dengyu.clicked.connect(self.on_calculate)
self.pushButton_dian.clicked.connect(self.on_calculate)
self.pushButton_jia.clicked.connect(self.on_calculate)
self.pushButton_jian.clicked.connect(self.on_calculate)
self.pushButton_cheng.clicked.connect(self.on_calculate)
self.pushButton_chu.clicked.connect(self.on_calculate)
self.pushButton_back.clicked.connect(self.on_calculate)
self.pushButton_clear.clicked.connect(self.on_calculate)
self.pushButton_zuo.clicked.connect(self.on_calculate)
self.pushButton_you.clicked.connect(self.on_calculate)
2、增加一个类变量expression
用来将每次输入的字符合在一起成为一个计算表达式,例如分别输入‘3’,‘+’,‘5’组成的计算表达式为:3+5
self.expression = ""
3、计算函数的逻辑:
1)收集到的字符为"01234567890()+-*/."
将其组合在一起形成一个计算表达式
2)收集到的字符串为"="
将前面组合成的表达式通过eval()函数进行计算,将之转换为字符串作为结果显示出来
3)收集到的字符串为"←"
将前面组合成的表达式减去最后一个字符并显示出来
4)收集到的字符串为"CE"
将前面累积的表达式置为空字符串并显示出来
三、附上整体代码:
main.py
from PyQt5.QtWidgets import QApplication
from grid import Grid
import sys
app = QApplication(sys.argv)
win = Grid()
win.show()
sys.exit(app.exec_())
grid.py
from PyQt5.QtWidgets import QMainWindow
from grid_ui import Ui_MainWindow
from PyQt5 import QtCore
class Grid(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Grid, self).__init__(parent)
self.setupUi(self)
self.expression = ""
self.pushButton_000.clicked.connect(self.on_calculate)
self.pushButton_111.clicked.connect(self.on_calculate)
self.pushButton_222.clicked.connect(self.on_calculate)
self.pushButton_333.clicked.connect(self.on_calculate)
self.pushButton_444.clicked.connect(self.on_calculate)
self.pushButton_555.clicked.connect(self.on_calculate)
self.pushButton_666.clicked.connect(self.on_calculate)
self.pushButton_777.clicked.connect(self.on_calculate)
self.pushButton_888.clicked.connect(self.on_calculate)
self.pushButton_999.clicked.connect(self.on_calculate)
self.pushButton_dengyu.clicked.connect(self.on_calculate)
self.pushButton_dian.clicked.connect(self.on_calculate)
self.pushButton_jia.clicked.connect(self.on_calculate)
self.pushButton_jian.clicked.connect(self.on_calculate)
self.pushButton_cheng.clicked.connect(self.on_calculate)
self.pushButton_chu.clicked.connect(self.on_calculate)
self.pushButton_back.clicked.connect(self.on_calculate)
self.pushButton_clear.clicked.connect(self.on_calculate)
self.pushButton_zuo.clicked.connect(self.on_calculate)
self.pushButton_you.clicked.connect(self.on_calculate)
grid_ui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'grid_ui.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(803, 691)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(120, 80, 411, 281))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setSpacing(6)
self.gridLayout.setObjectName("gridLayout")
self.pushButton_777 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_777.setObjectName("pushButton_777")
self.gridLayout.addWidget(self.pushButton_777, 1, 0, 1, 1)
self.pushButton_444 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_444.setObjectName("pushButton_444")
self.gridLayout.addWidget(self.pushButton_444, 2, 0, 1, 1)
self.pushButton_111 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_111.setObjectName("pushButton_111")
self.gridLayout.addWidget(self.pushButton_111, 3, 0, 1, 1)
self.pushButton_333 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_333.setObjectName("pushButton_333")
self.gridLayout.addWidget(self.pushButton_333, 3, 2, 1, 1)
self.pushButton_cheng = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_cheng.setObjectName("pushButton_cheng")
self.gridLayout.addWidget(self.pushButton_cheng, 2, 3, 1, 1)
self.pushButton_666 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_666.setObjectName("pushButton_666")
self.gridLayout.addWidget(self.pushButton_666, 2, 2, 1, 1)
self.pushButton_999 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_999.setObjectName("pushButton_999")
self.gridLayout.addWidget(self.pushButton_999, 1, 2, 1, 1)
self.pushButton_555 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_555.setObjectName("pushButton_555")
self.gridLayout.addWidget(self.pushButton_555, 2, 1, 1, 1)
self.pushButton_jia = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_jia.setObjectName("pushButton_jia")
self.gridLayout.addWidget(self.pushButton_jia, 4, 3, 1, 1)
self.pushButton_jian = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_jian.setObjectName("pushButton_jian")
self.gridLayout.addWidget(self.pushButton_jian, 3, 3, 1, 1)
self.pushButton_888 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_888.setObjectName("pushButton_888")
self.gridLayout.addWidget(self.pushButton_888, 1, 1, 1, 1)
self.pushButton_chu = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_chu.setObjectName("pushButton_chu")
self.gridLayout.addWidget(self.pushButton_chu, 1, 3, 1, 1)
self.pushButton_222 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_222.setObjectName("pushButton_222")
self.gridLayout.addWidget(self.pushButton_222, 3, 1, 1, 1)
self.pushButton_back = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_back.setObjectName("pushButton_back")
self.gridLayout.addWidget(self.pushButton_back, 0, 3, 1, 1)
self.pushButton_clear = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_clear.setObjectName("pushButton_clear")
self.gridLayout.addWidget(self.pushButton_clear, 0, 2, 1, 1)
self.pushButton_you = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_you.setObjectName("pushButton_you")
self.gridLayout.addWidget(self.pushButton_you, 0, 1, 1, 1)
self.pushButton_zuo = QtWidgets.QPushButton(self.gridLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_zuo.sizePolicy().hasHeightForWidth())
self.pushButton_zuo.setSizePolicy(sizePolicy)
self.pushButton_zuo.setSizeIncrement(QtCore.QSize(0, 0))
self.pushButton_zuo.setAutoRepeatInterval(100)
self.pushButton_zuo.setAutoDefault(False)
self.pushButton_zuo.setDefault(False)
self.pushButton_zuo.setFlat(False)
self.pushButton_zuo.setObjectName("pushButton_zuo")
self.gridLayout.addWidget(self.pushButton_zuo, 0, 0, 1, 1)
self.pushButton_000 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_000.setObjectName("pushButton_000")
self.gridLayout.addWidget(self.pushButton_000, 4, 0, 1, 1)
self.pushButton_dengyu = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_dengyu.setObjectName("pushButton_dengyu")
self.gridLayout.addWidget(self.pushButton_dengyu, 4, 2, 1, 1)
self.pushButton_dian = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_dian.setObjectName("pushButton_dian")
self.gridLayout.addWidget(self.pushButton_dian, 4, 1, 1, 1)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(120, 39, 411, 31))
self.lineEdit.setObjectName("lineEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 803, 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)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_777.setText(_translate("MainWindow", "7"))
self.pushButton_444.setText(_translate("MainWindow", "4"))
self.pushButton_111.setText(_translate("MainWindow", "1"))
self.pushButton_333.setText(_translate("MainWindow", "3"))
self.pushButton_cheng.setText(_translate("MainWindow", "*"))
self.pushButton_666.setText(_translate("MainWindow", "6"))
self.pushButton_999.setText(_translate("MainWindow", "9"))
self.pushButton_555.setText(_translate("MainWindow", "5"))
self.pushButton_jia.setText(_translate("MainWindow", "+"))
self.pushButton_jian.setText(_translate("MainWindow", "-"))
self.pushButton_888.setText(_translate("MainWindow", "8"))
self.pushButton_chu.setText(_translate("MainWindow", "/"))
self.pushButton_222.setText(_translate("MainWindow", "2"))
self.pushButton_back.setText(_translate("MainWindow", "←"))
self.pushButton_clear.setText(_translate("MainWindow", "CE"))
self.pushButton_you.setText(_translate("MainWindow", ")"))
self.pushButton_zuo.setText(_translate("MainWindow", "("))
self.pushButton_000.setText(_translate("MainWindow", "0"))
self.pushButton_dengyu.setText(_translate("MainWindow", "="))
self.pushButton_dian.setText(_translate("MainWindow", "."))