3
我有一個小程序,它使用SymPy的「漂亮打印」工具即時渲染輸入的方程。這工作正常,但不看起來很專業。由於SymPy會生成latex或mml,我想知道這些是否可以用PySide小部件以圖形方式呈現?我顯然需要改變'QTextBrowser()',但是我不確定。我知道諾基亞提供了QtMmlWidget,但我不確定PySide是否可以使用它。使用PySide渲染latex/mathml
非常感謝和最良好的祝願。
from __future__ import division
import sys
import sympy
from PySide.QtGui import *
from PySide.QtCore import *
from PySide.QtXml import *
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.browser = QTextBrowser()
self.browser.setCurrentFont(QFont("Courier New",10,QFont.Bold))
self.lineedit = QLineEdit("please type an expression")
self.lineedit.selectAll()
layout = QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.lineedit)
self.setLayout(layout)
self.lineedit.setFocus()
self.connect(self.lineedit, SIGNAL("textChanged (const QString&)"),self.updateUi)
def updateUi(self):
text = unicode(self.lineedit.text())
for z in range(0,9):
text = text.replace('x'+str(z),'x^'+str(z))
text = text.replace(')'+str(z),')^'+str(z))
text = text.replace(str(z)+'x',str(z)+'*x')
text = text.replace(str(z)+'(',str(z)+'*(')
try:
self.browser.append(sympy.printing.pretty(sympy.sympify(text)))
self.browser.clear()
self.browser.append(sympy.printing.pretty(sympy.sympify(text)))
except Exception:
if text=='': self.browser.clear()
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()