Search code examples
pythonformattingwxpython

How can I prevent wx.StaticText from removing consecutive whitespaces in it's input string?


I want to print text on a window such that the text is justified (like in MS-Word). when I input a string to wx.StaticText(panel, label=txt), it completely ignores duplicate spaces and replaces them with one space for each.

How to prevent wx.StaticText from modifying the input text? How can I get justified text using wx.StaticText? is there a way to override this behavior?

p.s. I am aware that I can draw directly on a panel using wx.PaintDC's methods, but it is tedious.

Here is a screenshot of the input string and what I get in the window: Justified text (left) vs wxpython output


Solution

  • wx.StaticText does not change the label string, but a monospaced font should be used for the lines of the paragraph to maintain vertical alignment. like so:

    font = wx.Font(pointSize=15, family=wx.FONTFAMILY_MODERN,\
                   style=wx.FONTSTYLE_NORMAL, weight=wx.FONTWEIGHT_NORMAL,\
                   faceName="Courier New")
    staticText = wx.StaticText(parentPanel, label=text, style=wx.ALIGN_LEFT)
    staticText.SetFont(font)