0

I am working on an application for which I want to include the functionality to display matrices to users. I have modified code from the following link to display equations: Displaying (nicely) an algebraic expression in PyQt

This works well for displaying equations (commented out in code). For displaying a matrix I can't get it to work though. I get the following error: error message:
error message

The example matrix looks like this in Jupyter Notebook. I'd like the output in pyqt5 to look the same. I'd like to know if there is a way to get this method to work, if not, is there an alternative (simple) approach? jupyter matrix output:
jupyter matrix output

Code to run the problem:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QComboBox, QPushButton, QVBoxLayout
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

import sympy
from sympy import*

class Pop_Up(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(50,50,600,300)
        self.setWindowTitle("Test")

        self.centralwidget = QtWidgets.QWidget(self)
        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 500, 250))
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        
        self._figure=Figure()      
        self._canvas=FigureCanvas(self._figure)
        
        self.verticalLayout.addWidget(self._canvas)
        
    def paintEvent(self,event):

        #equation strings
        Swedish_rolling=r'$[sin(\alpha + \beta + \gamma) , -cos(\alpha + \beta + \gamma) , -lcos(\beta + \gamma)] R(\theta) \dot{\xi}_I - r\dot{\varphi}cos(\gamma) = 0$'
        
        #Matrix input:
        theta = symbols('theta')
        Matrix1 = Matrix([[cos(theta),-sin(theta),0],[sin(theta),cos(theta),0],[0,0,1]])
        
        #clears or resets
        self._figure.clear() 
        
        #swap output strings to compare equation and matrix output
        #outputStr = Swedish_rolling
        outputStr = r"$" + f"{sympy.latex(Matrix1)}" + r"$"
        
        text=self._figure.suptitle(
            outputStr,
            x=0.0,
            y=1.0,
            horizontalalignment='left',
            verticalalignment='top',
            size=QtGui.QFont().pointSize()*1
        )
    
        self._canvas.draw()   
def window():
    app=QApplication(sys.argv)
    win = Pop_Up()
    win.show()
    sys.exit(app.exec_())
    
window()
eyllanesc
  • 221,139
  • 17
  • 121
  • 189
rwestga
  • 1
  • 1

0 Answers0