使用 PySide 渲染 Latex / mathml
我有一个小程序,可以使用 SymPy 的“漂亮打印”功能动态渲染输入的方程。这工作正常,但看起来不太专业。由于 SymPy 将生成 Latex 或 mml,我想知道这些是否可以使用 PySide 小部件以图形方式呈现?我显然需要更改“QTextBrowser()”,但我不确定。我知道诺基亚提供了 QtMmlWidget 但我不确定 PySide 是否可以使用它。
非常感谢和良好的祝愿。
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_()
I have a small program that renders a typed equation on the fly using SymPy's 'pretty-printing' facility. This works fine but doesn't look very professional. As SymPy will produce latex or mml I was wondering whether these could be rendered graphically with a PySide widget? I would obviously need to change the 'QTextBrowser()', but to what I'm not sure. I know Nokia provides QtMmlWidget but I'm not sure if this could be used by PySide.
Many thanks and best wishes.
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_()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种巧妙的方法是使用 MathJax 之类的东西在
QWebiew
中显示它,或者,受到这个问题的启发你可以使用SVGMath 模块 将 MathML 转换为 SVG,然后可以在
QSvgWidget
中显示One hacky way would be to display it in a
QWebiew
using something like MathJaxOr, inspired by this question you could use the SVGMath module to convert form MathML to SVG, which can then be displayed in a
QSvgWidget