pythonlabelwxpythonsizer

How to change wx.sbSizer label color and size in python?


I'm creating a GUI and I'm using several wx.sbSizer to group the widgets. However I need to change the font and the color of the wx.sbSizer Label ('MyApp') and although I have searched I cannot find out how to do it.

Here's a minimal working example:

    class MyFrame ( wx.Frame ):
        
        def __init__( self, parent ):
            wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
            
            self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
            
            bSizer1 = wx.BoxSizer( wx.VERTICAL )
            
            self.m_panel31 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
            bSizer1_2 = wx.BoxSizer( wx.VERTICAL )
            
            sbSizer1_2_1 = wx.StaticBoxSizer( wx.StaticBox( self.m_panel31, wx.ID_ANY, u"MyApp" ), wx.VERTICAL )
            
            self.m_button6 = wx.Button( sbSizer1_2_1.GetStaticBox(), wx.ID_ANY, u"1", wx.DefaultPosition, wx.DefaultSize, 0 )
            sbSizer1_2_1.Add( self.m_button6, 0, wx.ALL, 5 )
            
            self.m_button7 = wx.Button( sbSizer1_2_1.GetStaticBox(), wx.ID_ANY, u"2", wx.DefaultPosition, wx.DefaultSize, 0 )
            sbSizer1_2_1.Add( self.m_button7, 0, wx.ALL, 5 )
            
            
            bSizer1_2.Add( sbSizer1_2_1, 1, wx.EXPAND, 5 )
            
            
            self.m_panel31.SetSizer( bSizer1_2 )
            self.m_panel31.Layout()
            bSizer1_2.Fit( self.m_panel31 )
            bSizer1.Add( self.m_panel31, 1, wx.EXPAND |wx.ALL, 5 )
            
            
            self.SetSizer( bSizer1 )
            self.Layout()
            
            self.Centre( wx.BOTH )
        
        def __del__( self ):
            pass

if __name__ == "__main__":
    app = wx.App(redirect=False)
    frame = MyFrame(None)
    app.SetTopWindow(frame)
    frame.Show(True)
    app.MainLoop()

Thank you in adavnce for any help.


Solution

  • You are already using sbSizer1_2_1.GetStaticBox() to access the box within the sizer.
    Simply use that to get the box, then utilise the methods available for that widget, see below:

    import wx
    class MyFrame ( wx.Frame ):
            
        def __init__( self, parent ):
            wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
            
            self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
            
            bSizer1 = wx.BoxSizer( wx.VERTICAL )
            
            self.m_panel31 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
            bSizer1_2 = wx.BoxSizer( wx.VERTICAL )
            
            sbSizer1_2_1 = wx.StaticBoxSizer( wx.StaticBox( self.m_panel31, wx.ID_ANY, u"MyApp" ), wx.VERTICAL )
    
        # Get the box and then set the colour and font
            sb = sbSizer1_2_1.GetStaticBox()
            sb.SetBackgroundColour('lightgreen')
            font = wx.Font(16, wx.ROMAN, wx.ITALIC, wx.NORMAL) 
            sb.SetFont(font)
    
            self.m_button6 = wx.Button( sbSizer1_2_1.GetStaticBox(), wx.ID_ANY, u"1", wx.DefaultPosition, wx.DefaultSize, 0 )
            sbSizer1_2_1.Add( self.m_button6, 0, wx.ALL, 5 )
            
            self.m_button7 = wx.Button( sbSizer1_2_1.GetStaticBox(), wx.ID_ANY, u"2", wx.DefaultPosition, wx.DefaultSize, 0 )
            sbSizer1_2_1.Add( self.m_button7, 0, wx.ALL, 5 )
     
            bSizer1_2.Add( sbSizer1_2_1, 1, wx.EXPAND, 5 )
            
            self.m_panel31.SetSizer( bSizer1_2 )
            self.m_panel31.Layout()
            bSizer1_2.Fit( self.m_panel31 )
            bSizer1.Add( self.m_panel31, 1, wx.EXPAND |wx.ALL, 5 )
            
            self.SetSizer( bSizer1 )
            self.Layout()
            
            self.Centre( wx.BOTH )
        
        def __del__( self ):
            pass
    
    if __name__ == "__main__":
        app = wx.App(redirect=False)
        frame = MyFrame(None)
        app.SetTopWindow(frame)
        frame.Show(True)
        app.MainLoop()
    

    enter image description here