Super and Subscript using wxPython


The wxPython wx.lib.fancytext widget allows you to display super and subscripts in texts, and allows you to change fonts and colours too. All this is done with XML coded strings. XML documentation code is tag based and not that difficult to understand. It looks rather similar to the older HTML syntax used in web pages.

About the Author


# wxPython's wx.lib.fancytext can show super and subscripts, fonts and colours

import  wx
import  wx.lib.fancytext as fancytext

class FancyText(wx.Panel):
    """display fancytext on a panel"""
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnPaint(self, evt):
        """display the fancytext on a paintdc surface"""
        dc = wx.PaintDC(self)
        # need height to calculate y position of str2
        w, h = fancytext.GetExtent(xml_str1, dc)
        # enclose is True by default (start and end tags not needed)
        fancytext.RenderToDC(xml_str1, dc, 20, 20)
        fancytext.RenderToDC(xml_str2, dc, 20, 20 + h + 5)

# these are XML code strings
xml_str1 = ('<font style="italic" family="swiss" color="red" weight="bold" >'
            'some fancy text y = x<sup>0.5</sup>  now text<sub>with subscript</sub>'
            '   <times/> <infinity/> <angle/></font>  back to plain text')

xml_str2 = '<font family="swiss" color="blue" size="48">feeling blue?</font>'

# test FancyText
if __name__ == '__main__':
    app = wx.PySimpleApp()
    pos = wx.DefaultPosition
    frame = wx.Frame(None, -1, title='wxPython fancy text', pos=pos, size=(500, 200))
    tp = FancyText(frame)