在PyQt中显示(很好)一个代数表达式 [英] Displaying (nicely) an algebraic expression in PyQt

查看:217
本文介绍了在PyQt中显示(很好)一个代数表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的python程序中是我创建的代表数学公式的数学对象的层次结构. 例如,存在Collection,它是一系列Equations,它是两个SumsProductsQuotientsExponantsLogarithms的容器,它们都是,可以是数字或变量.

In my python program is a hierarchy of mathematical objects I've created, that represent mathematical formulae.
For example, there's Collection, which is a series of Equations, which is a container of two Sums, Products, Quotients, Exponants or Logarithms, which are all establishments upon Figures, which can be numerical or variables.

我需要向用户显示要执行的数学运算步骤,以完成某些任务,例如获取未知变量等.目前,我以纯文本格式生成此指南",就像...

I need to display to the user, a step of mathematical operations to perform to achieve certain tasks, such as getting unknown variables, etc. Currently, I generate this 'guide' in plain text, much like...

F = (m)(a)  
m = (F)/(a)  
a = 2, F = 3  
m = (3)/(2) = 1.5

但是,用纯文本表示复杂的公式(例如log[2](n)=(m)((a)^(2)))很丑陋,并且违反了程序的目标,即保持用户程序的最高清晰度.

However, representing complex formulas in plain text (eg: log[2](n)=(m)((a)^(2)) ) is ugly, and defeats a goal of the program, which is to maintain upmost clarity of procedure for the user.

我目前正在用PyQt4编写GUI,并且需要表示可以在Qt应用程序中显示的公式(每个步骤).公式不必在文本中.由于对象结构的缘故,根据需要将对象转换为标记文本非常容易.显示的实际方法可以是任何东西,只要我可以在GUI中将其干净地卡住即可,例如SVG,图像等.

I'm currently writing the GUI in PyQt4, and need to make a representation of the formulaes (each step) that can be displayed in a Qt Application. The formulaes do not need to be in text. Due to the object structure, it's extremely easy to convert the objects into mark up text as needed. The actual method of display can be anything, as long as I can cleanly chuck it in my GUI, such as an SVG, an image, etc.

  • 起初,我考虑过MathML,但读到PyQt在显示HTML时会忽略MathML标签,并且曾经有一个QMmlWidget用于此确切的任务,但已被弃用.
  • 然后我阅读到sympysvgmathQSvgWidget的组合有效,将公式显示为Svgs,但是我需要分发代码,这在设置svgmath时禁止字体配置.
  • 我认为这看起来很有希望:显示与终端一致的LaTeX方程,但这些视频的总播放时间将近4个小时,我对此没有耐心.
  • At first, I considered MathML, but read that PyQt ignores MathML tags when displaying HTML, and that a QMmlWidget once existed for this exact task, but has been deprecated.
  • I then read that a combination of sympy, svgmath and QSvgWidget worked, displaying the formulaes as Svgs, but I need my code to be distributable, which the font configuration stuff in setting up svgmath inhibits.
  • I thought this looked promising: Display LaTeX Equations in-line with terminal but the videos run a total length of almost 4 hours, for which I don't have the patience.

我几乎倾向于自己编写代码,使用PIL或类似的方法将我的数学对象直接转换为图像,但是我不希望有人已经做到了这一点,并且做得更好.

I'm almost inclined to code it myself, using PIL or something like that to convert my math objects straight to an image, but I'd hate to think somebody has already achieved this, with a better job.

非常感谢您的帮助!
谢谢!

Any and all help is extremely appreciated!
Thanks!

推荐答案

来自博客

From the blog Gulon.co.uk. She's provided a simple yet powerful example for rendering LateX equations in pyQt. I have copied from there and pasted below:

from PyQt4.QtCore import *

from PyQt4.QtGui import *

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas


class MathTextLabel(QWidget):

    def __init__(self, mathText, parent=None, **kwargs):
        QWidget.__init__(self, parent, **kwargs)

        l=QVBoxLayout(self)
        l.setContentsMargins(0,0,0,0)

        r,g,b,a=self.palette().base().color().getRgbF()

    self._figure=Figure(edgecolor=(r,g,b), facecolor=(r,g,b))
    self._canvas=FigureCanvas(self._figure)
    l.addWidget(self._canvas)

    self._figure.clear()
    text=self._figure.suptitle(
        mathText,
        x=0.0,
        y=1.0,
        horizontalalignment='left',
        verticalalignment='top',
        size=qApp.font().pointSize()*2)
    self._canvas.draw()

    (x0,y0),(x1,y1)=text.get_window_extent().get_points()
    w=x1-x0; h=y1-y0

    self._figure.set_size_inches(w/80, h/80)
    self.setFixedSize(w,h)

if __name__=='__main__':
    from sys import argv, exit

    class Widget(QWidget):
        def __init__(self, parent=None, **kwargs):
            QWidget.__init__(self, parent, **kwargs)

            l=QVBoxLayout(self)
            l.addWidget(QLabel("<h1>Discrete Fourier Transform</h1>"))

            mathText=r'$X_k = \sum_{n=0}^{N-1} x_n . e^{\frac{-i2\pi kn}{N}}$'
            l.addWidget(MathTextLabel(mathText, self),     alignment=Qt.AlignHCenter)

    a=QApplication(argv)
    w=Widget()
    w.show()
    w.raise_()
    exit(a.exec_())


已翻译为PyQt5


Translated to PyQt5

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtCore import Qt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas


class MathTextLabel(QtWidgets.QWidget):

    def __init__(self, mathText, parent=None, **kwargs):
        super(QtWidgets.QWidget, self).__init__(parent, **kwargs)

        l=QVBoxLayout(self)
        l.setContentsMargins(0,0,0,0)

        r,g,b,a=self.palette().base().color().getRgbF()

        self._figure=Figure(edgecolor=(r,g,b), facecolor=(r,g,b))
        self._canvas=FigureCanvas(self._figure)
        l.addWidget(self._canvas)
        self._figure.clear()
        text=self._figure.suptitle(
            mathText,
            x=0.0,
            y=1.0,
            horizontalalignment='left',
            verticalalignment='top',
            size=QtGui.QFont().pointSize()*2
        )
        self._canvas.draw()

        (x0,y0),(x1,y1)=text.get_window_extent().get_points()
        w=x1-x0; h=y1-y0

        self._figure.set_size_inches(w/80, h/80)
        self.setFixedSize(w,h)

if __name__=='__main__':
    from sys import argv, exit

    class Widget(QtWidgets.QWidget):
        def __init__(self, parent=None, **kwargs):
            super(QtWidgets.QWidget, self).__init__(parent, **kwargs)

            l=QVBoxLayout(self)
            mathText=r'$X_k = \sum_{n=0}^{N-1} x_n . e^{\frac{-i2\pi kn}{N}}$'
            l.addWidget(MathTextLabel(mathText, self), alignment=Qt.AlignHCenter)

    a=QtWidgets.QApplication(argv)
    w=Widget()
    w.show()
    w.raise_()
    exit(a.exec_())

这篇关于在PyQt中显示(很好)一个代数表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆