Start New Discussion within our Software Development Community

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.

# 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.SetBackgroundColour('white')
        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)
    frame.Show(True)
    app.MainLoop()
The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.