简单计算器(PyQt5 + Qt Designer)

Posted er-gou-zi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单计算器(PyQt5 + Qt Designer)相关的知识,希望对你有一定的参考价值。

Qt Designer设计完是这样的(基本就是16个Push Button和1个lineEdit,再加上尺寸调整以及简单的布局)

技术图片

设计结束后,保存为name.ui(比如calculator.ui),打开cmd,cd到name.ui所在文件夹,执行以下代码:pyuic5 -o name.py name.ui,生成name.py

打开name.py(比如Spyder),新建main.py,执行如下代码

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from calculator import Ui_MainWindow

class Calcu(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.connection()
        self.show()
    
    def num_0(self):
        self.lineEdit.insert(0)
    
    def num_1(self):
        self.lineEdit.insert(1)
        
    def num_2(self):
        self.lineEdit.insert(2)
        
    def num_3(self):
        self.lineEdit.insert(3)
        
    def num_4(self):
        self.lineEdit.insert(4)
        
    def num_5(self):
        self.lineEdit.insert(5)
        
    def num_6(self):
        self.lineEdit.insert(6)
        
    def num_7(self):
        self.lineEdit.insert(7)
        
    def num_8(self):
        self.lineEdit.insert(8)
    
    def num_9(self):
        self.lineEdit.insert(9)
    
    def op_plus(self):
        self.lineEdit.insert(+)
        
    def op_minus(self):
        self.lineEdit.insert(-)
        
    def op_multiply(self):
        self.lineEdit.insert(*)
    
    def op_divide(self):
        self.lineEdit.insert(/)
    
    def op_ce(self):
        self.lineEdit.clear()
        
    def calculate(self):
        text = self.lineEdit.text()
        try:
            self.lineEdit.setText(str(eval(text)))
        except:
            self.lineEdit.setText(invalid syntax, check your input!)
        
    def connection(self):
        self.Num_0.clicked.connect(self.num_0)
        self.Num_1.clicked.connect(self.num_1)
        self.Num_2.clicked.connect(self.num_2)
        self.Num_3.clicked.connect(self.num_3)
        self.Num_4.clicked.connect(self.num_4)
        self.Num_5.clicked.connect(self.num_5)
        self.Num_6.clicked.connect(self.num_6)
        self.Num_7.clicked.connect(self.num_7)
        self.Num_8.clicked.connect(self.num_8)
        self.Num_9.clicked.connect(self.num_9)
        self.OP_plus.clicked.connect(self.op_plus)
        self.OP_minus.clicked.connect(self.op_minus)
        self.OP_multiply.clicked.connect(self.op_multiply)
        self.OP_divide.clicked.connect(self.op_divide)
        self.OP_CE.clicked.connect(self.op_ce)
        self.OP_equal.clicked.connect(self.calculate)

if __name__ == __main__:
    
    app = QApplication(sys.argv)
    ca = Calcu()
    sys.exit(app.exec_())

结果如下:

技术图片

 

-----------------------------------------------------------------------------------------------------The end

以上是关于简单计算器(PyQt5 + Qt Designer)的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5 图形界面 - Qt Designer设置简体中文方法演示,Qt Designer字体设置,Qt Designer工具单独安装包获取,Qt Designer简体中文语言包获取

PyCharm+PyQt5+Qt Designer配置

PyQt5配合Qt-Designer实现GUI

Pycharm安装PyQt5和pyqt5-tools从而使用Qt Designer

pyqt5.8.2没有qt Designer和assistant exe

pyqt5 Qt Designer + matpoltlib画图显示动态曲线