如何在 python 中更改 wx.sbSizer 标签颜色和大小?

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

我正在创建一个 GUI,我正在使用几个 wx.sbSizer 来对小部件进行分组。但是,我需要更改 wx.sbSizer 标签 ('MyApp') 的字体和颜色,虽然我已经搜索过了,但我找不到如何操作的方法。

这是一个最小的工作示例:

    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()

在此感谢您的帮助。

您已经在使用 sbSizer1_2_1.GetStaticBox() 访问 sizer 中的框。
只需使用它来获取框,然后利用该小部件可用的方法,请参见下文:

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()