Search code examples
wxpython

Text padding inside wx.StaticText


I have a wx.StaticText control inside my wxpython phoenix app. I want the text to appear on a certain background color and have some "air" or padding around the text. Doing a SetBackgroudnColour(RED) does indeed set the correct background color. But how to give the text inside some more room ?

UPDATE: See image below to see what I mean.

enter image description here

I guess this could be done using resizing the textbox and event bindings to resize the textbox if needed. But my attempts like below did not give a proper result (the sizer in which this component is in does not layout properly). Any help welcome.

    def _on_resize(self,event):
        try:
            size = self.text_box.GetSize()
            _y = 2 * size[1]
            self.text_box.SetSize(wx.Size(size[0], _y))

        except AttributeError:
            pass
        event.Skip()

Solution

  • You keep mentioning textbox but I assume that you do not mean a wx.StaticBox as it is never specifically mentioned. Note that there is a wx.StaticBoxSizer if you are referring to a StaticBox.
    You can always just pad out a vertical boxsizer and perhaps fiddle with the font size to adjust the "air" around your text.
    Here is an ugly example:

    import sys
    sys.path.insert(0,'/usr/lib/python3/dist-packages')
    import wx
    
    class MyFrame(wx.Frame):
        def __init__(self, title):
            wx.Frame.__init__(self, None, title=title)
            self.panel = wx.Panel(self, wx.ID_ANY)
            text1 = wx.StaticText(self, -1, 'Tight Text')
            text1.SetForegroundColour(wx.WHITE)
            text1.SetBackgroundColour(wx.RED)
    
            text2 = wx.StaticText(self, -1, "  Loose text in a box  ")
            text3 = wx.StaticText(self, -1, "  Tighter text in a box  ")
            text4 = wx.TextCtrl(self, -1, "Tighter text in a box", size=(130,30), style=wx.TE_READONLY|wx.TE_CENTRE)
            t21 = wx.StaticText(self, -1, "")
            t22 = wx.StaticText(self, -1, "")
            t31 = wx.StaticText(self, -1, "")
            t32 = wx.StaticText(self, -1, "")
    
            text2.SetBackgroundColour(wx.RED)
            text2.SetForegroundColour(wx.WHITE)
            text3.SetBackgroundColour(wx.RED)
            text3.SetForegroundColour(wx.WHITE)
            text4.SetForegroundColour(wx.WHITE)
    
            t21.SetBackgroundColour(wx.RED)
            t22.SetBackgroundColour(wx.RED)
            t31.SetBackgroundColour(wx.RED)
            t32.SetBackgroundColour(wx.RED)
            text4.SetBackgroundColour(wx.RED)
    
            sbox=wx.BoxSizer(wx.VERTICAL)
            sbox.Add(t21,0 ,wx.EXPAND,0)
            sbox.Add(text2, 0, wx.EXPAND, 0)
            sbox.Add(t22,0,wx.EXPAND,0)
    
            font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
            font.SetPointSize(2)
            sbox1=wx.BoxSizer(wx.VERTICAL)
            t31.SetFont(font)
            t32.SetFont(font)
            sbox1.Add(t31,0 ,wx.EXPAND,0)
            sbox1.Add(text3, 0, wx.EXPAND, 0)
            sbox1.Add(t32,0,wx.EXPAND,0)
    
            self.Bind(wx.EVT_CLOSE, self.OnClose)
            sizer=wx.BoxSizer(wx.VERTICAL)
            sizer.Add(text1,0,wx.ALL,5)
            sizer.Add(sbox,0,wx.ALL,5)
            sizer.Add(sbox1,0,wx.ALL,5)
            sizer.Add(text4,0,wx.ALL,5)
            self.SetSizer(sizer)
            self.Show()
    
        def OnClose(self, event):
            self.Destroy()
    
    app = wx.App()
    top = MyFrame("My Window")
    app.MainLoop()
    

    Please ignore my import statement as under '4.0.4 gtk2 (phoenix) wxWidgets 3.0.5' on Linux, setting background colour for static texts seems to fail completely and I am forcing a different version of wx

    enter image description here