PyQt5 实现矩阵计算器

为什么要实现矩阵计算器?
答:因为可以带来便利,相当于把matlib中对矩阵的基本操作抽离出来,降低操作门槛,同时也更加灵活自由,代码可以自己控制,功能可以自己添加修改。
–参考了pyqt5的example中的calculator。有关矩阵的库主要使用了sympy,参考了大佬的博客:sympy矩阵操作

计算器样子,丑媳妇早晚要见大大们, 后续我学好了qss和style 再优化它~/滑稽/逃:
在这里插入图片描述
言归正传,输入,输出格式举例(转置):
在这里插入图片描述

一些想法:
1.实现计算器之前可以先在https://www.draw.io/上绘制模型,这样布局的时候会更有底气,如:
在这里插入图片描述
2.如果想要把他变成exe,先

pip install pyinstaller

然后:

 pyinstaller -F -w -p /d/software/python3.7.4/lib calc.py

-F是生产单个文件,-w是图像界面显示(注意:需要在windows下),-p指出python lib的位置
之后运行会报错,没事不慌,
打开生产的 calc.spec,在第二、三行插入

import sys
sys.setrecursionlimit(5000)

最后:

pyinstaller calc.spec

就在dist文件夹下生产了calc.exe

矩阵计算器代码:

#!/usr/bin/python3
# -*- encoding_utf-8 -*-
import sys
sys.setrecursionlimit(5000) #为了用pyinstaller生产exe添加
if hasattr(sys, 'frozen'):	#为了生产exe添加
    os.environ['PATH'] = sys._MEIPASS + ";" + os.environ['PATH']
from PyQt5.QtWidgets import (QToolButton, QFrame, QVBoxLayout, QHBoxLayout, QGridLayout,
                             QApplication, QTextEdit, QSizePolicy, QLineEdit)
from PyQt5.QtGui import QIntValidator, QTextOption, QTextCursor
from sympy import symbols, Matrix, solve, diff, eye

class Button(QToolButton):
    def __init__(self, text, parent=None):
        super(Button, self).__init__(parent)

        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        self.setText(text)

    def sizeHint(self):
        size = super(Button, self).sizeHint()
        size.setHeight(size.height() + 50)
        size.setWidth(max(size.width(), size.height()))
        return size     
        
class Calc(QFrame):
    NumDigitButtons = 10
    def __init__(self):
        super().__init__()
        self.initData()
        self.initUI()
        self.initConnect()

    def initData(self):
        pass

    def initUI(self):
        self.layout = QVBoxLayout()
        self.createDisplayBox()
        self.createInputBox()

        self.setLayout(self.layout)
        self.show()

    def initConnect(self):
        self.input.textChanged.connect(self.cursorChange)

    def cursorChange(self):
        print('cursorChange')
        cursor =  self.input.textCursor()
        cursor.movePosition(QTextCursor.End)
        self.input.setTextCursor(cursor)

    def createDisplayBox(self):
        layout = QHBoxLayout()
        self.input = QTextEdit()
        self.output = QTextEdit()
        self.output.setReadOnly(True) 
        self.output.setWordWrapMode(QTextOption.NoWrap)

        layout.addWidget(self.input)
        layout.addWidget(self.output)
        layout.setStretch(1.5, 1)
        self.layout.addLayout(layout)

    def createInputBox(self):
        layout = QGridLayout()
        self.digitButtons = []
        for i in range(Calc.NumDigitButtons):
            self.digitButtons.append(self.createButton(str(i),
                    self.digitClicked))

        self.reverseButton = self.createButton("求逆",
                self.reverseOperateClicked)
        self.powButton =  self.createButton("次方:",
                self.powOperatorClicked)
        self.powLineEdit = QLineEdit
  • 7
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的 PyQt5 计算器实现: ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, QPushButton, QLineEdit import sys class Calculator(QMainWindow): def __init__(self): super().__init__() # 设置窗口标题和大小 self.setWindowTitle('Calculator') self.setFixedSize(240, 320) # 创建中心窗口和布局 self.central_widget = QWidget() self.setCentralWidget(self.central_widget) self.central_layout = QVBoxLayout() self.central_widget.setLayout(self.central_layout) # 创建文本框和按钮 self.display = QLineEdit() self.display.setReadOnly(True) self.central_layout.addWidget(self.display) self.buttons = [] button_layout = QGridLayout() button_layout.setSpacing(5) for i in range(0, 10): button = QPushButton(str(i)) button.clicked.connect(self.button_clicked) button_layout.addWidget(button, int((9-i)/3), (i-1)%3) self.buttons.append(button) self.add_button = QPushButton('+') self.add_button.clicked.connect(self.button_clicked) button_layout.addWidget(self.add_button, 0, 3) self.sub_button = QPushButton('-') self.sub_button.clicked.connect(self.button_clicked) button_layout.addWidget(self.sub_button, 1, 3) self.mul_button = QPushButton('*') self.mul_button.clicked.connect(self.button_clicked) button_layout.addWidget(self.mul_button, 2, 3) self.div_button = QPushButton('/') self.div_button.clicked.connect(self.button_clicked) button_layout.addWidget(self.div_button, 3, 3) self.dot_button = QPushButton('.') self.dot_button.clicked.connect(self.button_clicked) button_layout.addWidget(self.dot_button, 3, 2) self.eq_button = QPushButton('=') self.eq_button.clicked.connect(self.button_clicked) button_layout.addWidget(self.eq_button, 3, 1) self.clear_button = QPushButton('C') self.clear_button.clicked.connect(self.clear_display) button_layout.addWidget(self.clear_button, 3, 0) self.central_layout.addLayout(button_layout) def button_clicked(self): sender = self.sender() if sender in self.buttons: self.display.setText(self.display.text() + sender.text()) elif sender == self.dot_button: if '.' not in self.display.text(): self.display.setText(self.display.text() + '.') elif sender == self.add_button: self.display.setText(self.display.text() + '+') elif sender == self.sub_button: self.display.setText(self.display.text() + '-') elif sender == self.mul_button: self.display.setText(self.display.text() + '*') elif sender == self.div_button: self.display.setText(self.display.text() + '/') elif sender == self.eq_button: try: result = eval(self.display.text()) self.display.setText(str(result)) except: self.display.setText('Error') def clear_display(self): self.display.setText('') if __name__ == '__main__': app = QApplication(sys.argv) calculator = Calculator() calculator.show() sys.exit(app.exec_()) ``` 这个计算器有基本的加减乘除、小数点和等于按钮,并且能够处理异常情况。运行这个程序将会出现一个简单的计算器界面。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值