PyQt5和Qt designer做简易计算器

写在前面

初学PyQt5, 参考一篇博客做了一个简易计算器。
原博客传送门:用PyQt5开发一个简易的计算器

1.Qt designer设计界面

我用的是Eric6 里面带的Qt designer,首先设计计算器的界面,记得将各个PushButton之类的重新命名,如下图:
PyQt5和Qt designer做简易计算器_第1张图片
先在Qt designer里面设计好界面后保存,然后编译将.ui文件转化出一份.py文件。(右键点击.ui文件第一项compile form,然后发现项目底下多了一个.py文件)
PyQt5和Qt designer做简易计算器_第2张图片
由于我不太喜欢Eric 6 的IDE,我在sublime text里重新新建了一个project,然后将得到的.py文件复制一份。刚才得到的文件是可以直接运行来得到图形窗口的,但是没有编写逻辑,所以还不能执行功能。
Qt designer编译得到的代码:

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

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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(337, 425)
        self.verticalLayout_5 = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout_5.setObjectName("verticalLayout_5")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setMinimumSize(QtCore.QSize(0, 80))
        self.lineEdit.setObjectName("lineEdit")
        self.verticalLayout_5.addWidget(self.lineEdit)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.verticalLayout_4 = QtWidgets.QVBoxLayout()
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        self.Num_1 = QtWidgets.QPushButton(Form)
        self.Num_1.setMinimumSize(QtCore.QSize(70, 70))
        self.Num_1.setMaximumSize(QtCore.QSize(70, 70))
        self.Num_1.setObjectName("Num_1")
        self.verticalLayout_4.addWidget(self.Num_1)
        self.Num_4 = QtWidgets.QPushButton(Form)
        self.Num_4.setMinimumSize(QtCore.QSize(70, 70))
        self.Num_4.setMaximumSize(QtCore.QSize(70, 70))
        self.Num_4.setObjectName("Num_4")
        self.verticalLayout_4.addWidget(self.Num_4)
        self.Num_7 = QtWidgets.QPushButton(Form)
        self.Num_7.setMinimumSize(QtCore.QSize(70, 70))
        self.Num_7.setMaximumSize(QtCore.QSize(70, 70))
        self.Num_7.setObjectName("Num_7")
        self.verticalLayout_4.addWidget(self.Num_7)
        self.OP_plus = QtWidgets.QPushButton(Form)
        self.OP_plus.setMinimumSize(QtCore.QSize(70, 70))
        self.OP_plus.setMaximumSize(QtCore.QSize(70, 70))
        self.OP_plus.setObjectName("OP_plus")
        self.verticalLayout_4.addWidget(self.OP_plus)
        self.horizontalLayout.addLayout(self.verticalLayout_4)
        self.verticalLayout_3 = QtWidgets.QVBoxLayout()
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.Num_2 = QtWidgets.QPushButton(Form)
        self.Num_2.setMinimumSize(QtCore.QSize(70, 70))
        self.Num_2.setMaximumSize(QtCore.QSize(70, 70))
        self.Num_2.setObjectName("Num_2")
        self.verticalLayout_3.addWidget(self.Num_2)
        self.Num_5 = QtWidgets.QPushButton(Form)
        self.Num_5.setMinimumSize(QtCore.QSize(70, 70))
        self.Num_5.setMaximumSize(QtCore.QSize(70, 70))
        self.Num_5.setObjectName("Num_5")
        self.verticalLayout_3.addWidget(self.Num_5)
        self.Num_8 = QtWidgets.QPushButton(Form)
        self.Num_8.setMinimumSize(QtCore.QSize(70, 70))
        self.Num_8.setMaximumSize(QtCore.QSize(70, 70))
        self.Num_8.setObjectName("Num_8")
        self.verticalLayout_3.addWidget(self.Num_8)
        self.Num_0 = QtWidgets.QPushButton(Form)
        self.Num_0.setMinimumSize(QtCore.QSize(70, 70))
        self.Num_0.setMaximumSize(QtCore.QSize(70, 70))
        self.Num_0.setObjectName("Num_0")
        self.verticalLayout_3.addWidget(self.Num_0)
        self.horizontalLayout.addLayout(self.verticalLayout_3)
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.Num_3 = QtWidgets.QPushButton(Form)
        self.Num_3.setMinimumSize(QtCore.QSize(70, 70))
        self.Num_3.setMaximumSize(QtCore.QSize(70, 70))
        self.Num_3.setObjectName("Num_3")
        self.verticalLayout_2.addWidget(self.Num_3)
        self.Num_6 = QtWidgets.QPushButton(Form)
        self.Num_6.setMinimumSize(QtCore.QSize(70, 70))
        self.Num_6.setMaximumSize(QtCore.QSize(70, 70))
        self.Num_6.setObjectName("Num_6")
        self.verticalLayout_2.addWidget(self.Num_6)
        self.Num_9 = QtWidgets.QPushButton(Form)
        self.Num_9.setMinimumSize(QtCore.QSize(70, 70))
        self.Num_9.setMaximumSize(QtCore.QSize(70, 70))
        self.Num_9.setObjectName("Num_9")
        self.verticalLayout_2.addWidget(self.Num_9)
        self.OP_minus = QtWidgets.QPushButton(Form)
        self.OP_minus.setMinimumSize(QtCore.QSize(70, 70))
        self.OP_minus.setMaximumSize(QtCore.QSize(70, 70))
        self.OP_minus.setObjectName("OP_minus")
        self.verticalLayout_2.addWidget(self.OP_minus)
        self.horizontalLayout.addLayout(self.verticalLayout_2)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.CE = QtWidgets.QPushButton(Form)
        self.CE.setMinimumSize(QtCore.QSize(70, 70))
        self.CE.setMaximumSize(QtCore.QSize(70, 70))
        self.CE.setObjectName("CE")
        self.verticalLayout.addWidget(self.CE)
        self.OP_multi = QtWidgets.QPushButton(Form)
        self.OP_multi.setMinimumSize(QtCore.QSize(70, 70))
        self.OP_multi.setMaximumSize(QtCore.QSize(70, 70))
        self.OP_multi.setObjectName("OP_multi")
        self.verticalLayout.addWidget(self.OP_multi)
        self.OP_devide = QtWidgets.QPushButton(Form)
        self.OP_devide.setMinimumSize(QtCore.QSize(70, 70))
        self.OP_devide.setMaximumSize(QtCore.QSize(70, 70))
        self.OP_devide.setObjectName("OP_devide")
        self.verticalLayout.addWidget(self.OP_devide)
        self.OP_equal = QtWidgets.QPushButton(Form)
        self.OP_equal.setMinimumSize(QtCore.QSize(70, 70))
        self.OP_equal.setMaximumSize(QtCore.QSize(70, 70))
        self.OP_equal.setObjectName("OP_equal")
        self.verticalLayout.addWidget(self.OP_equal)
        self.horizontalLayout.addLayout(self.verticalLayout)
        self.verticalLayout_5.addLayout(self.horizontalLayout)

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

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.Num_1.setText(_translate("Form", "1"))
        self.Num_4.setText(_translate("Form", "4"))
        self.Num_7.setText(_translate("Form", "7"))
        self.OP_plus.setText(_translate("Form", "+"))
        self.Num_2.setText(_translate("Form", "2"))
        self.Num_5.setText(_translate("Form", "5"))
        self.Num_8.setText(_translate("Form", "8"))
        self.Num_0.setText(_translate("Form", "0"))
        self.Num_3.setText(_translate("Form", "3"))
        self.Num_6.setText(_translate("Form", "6"))
        self.Num_9.setText(_translate("Form", "9"))
        self.OP_minus.setText(_translate("Form", "-"))
        self.CE.setText(_translate("Form", "CE"))
        self.OP_multi.setText(_translate("Form", "*"))
        self.OP_devide.setText(_translate("Form", "/"))
        self.OP_equal.setText(_translate("Form", "="))

2.逻辑代码的编写

逻辑代码如下:

from Ui_calUI import Ui_Form
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QMainWindow

class Cacular(QWidget, Ui_Form):
    """docstring for Cacular"""
    def __init__(self):
        super(Cacular, self).__init__()
        self.setupUi(self)
        self.connecter()
        self.show()

    def ps_CE(self):
        self.lineEdit.clear()

    def ps_Num_1(self):
        self.lineEdit.insert('1')

    def ps_Num_0(self):
        self.lineEdit.insert('0')

    def ps_Num_2(self):
        self.lineEdit.insert('2')

    def ps_Num_3(self):
        self.lineEdit.insert('3')

    def ps_Num_4(self):
        self.lineEdit.insert('4')

    def ps_Num_5(self):
        self.lineEdit.insert('5')

    def ps_Num_6(self):
        self.lineEdit.insert('6')

    def ps_Num_7(self):
        self.lineEdit.insert('7')

    def ps_Num_8(self):
        self.lineEdit.insert('8')

    def ps_Num_9(self):
        self.lineEdit.insert('9')

    def ps_plus(self):
        self.lineEdit.insert('+')

    def ps_minus(self):
        self.lineEdit.insert('-')

    def ps_multi(self):
        self.lineEdit.insert('*')

    def ps_devide(self):
        self.lineEdit.insert('/')

    def connecter(self):
        self.Num_0.clicked.connect(self.ps_Num_0)
        self.Num_1.clicked.connect(self.ps_Num_1)
        self.Num_2.clicked.connect(self.ps_Num_2)
        self.Num_3.clicked.connect(self.ps_Num_3)
        self.Num_4.clicked.connect(self.ps_Num_4)
        self.Num_5.clicked.connect(self.ps_Num_5)
        self.Num_6.clicked.connect(self.ps_Num_6)
        self.Num_7.clicked.connect(self.ps_Num_7)
        self.Num_8.clicked.connect(self.ps_Num_8)
        self.Num_9.clicked.connect(self.ps_Num_9)
        self.OP_plus.clicked.connect(self.ps_plus)
        self.OP_minus.clicked.connect(self.ps_minus)
        self.OP_multi.clicked.connect(self.ps_multi)
        self.OP_devide.clicked.connect(self.ps_devide)
        self.OP_equal.clicked.connect(self.calcu)
        self.CE.clicked.connect(self.ps_CE)

    def calcu(self):
        text = self.lineEdit.text()
        try:
            result = eval(text)
            self.lineEdit.setText(str(eval(text)))
        except:
            self.lineEdit.setText('invalid syntax, check your input!')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    Ca = Cacular()
    sys.exit(app.exec_())

首先将Qt designer中编译出的文件import进来,然后只要将里面的各个PushButton通过信号和槽机制连接到自己编写的函数就行了。这样就实现了点击计算器的某个按钮就在lineEdit中显示了。
点击 = 时将记录的值通过 eval()函数计算出来。eval()函数能够将一个字符串计算出结果,这里用了try结构,防止程序因输入错误导致中断。
输入不正确的计算式时会有以下提示:
PyQt5和Qt designer做简易计算器_第3张图片
最后程序总共是两个文件,一个负责图形界面显示,一个负责逻辑实现。
PyQt5和Qt designer做简易计算器_第4张图片
即左侧两个文件。

3.总结

以上运用Qt designer制作了一个简易的计算器,目前测试没有发现问题。
之前学习python爬虫,现在想用python做一些简单的GUI,目前处于初学阶段,由于学的很多东西都容易忘,所以打算写一些博客来记录下自己的学习过程,也当做是笔记了。看有时间能不能整理出之前学的爬虫…

初次写博客,多多指教

你可能感兴趣的:(PyQt5)