I have a wx.StaticBox inside a wx.Panel. The staticbox is taking up virtually all of the space in the panel with no border. How can I add a border around the staticbox?

Some example code:

  self.aPanel = wx.Panel( self, wx.ID_ANY, style = wx.RAISED_BORDER )

  self.aLabel = wx.StaticText( self.aPanel, -1, 'Bar' )

  self.aSizer = wx.BoxSizer( wx.HORIZONTAL )
  self.aSizer.Add( self.aLabel, 0, wx.ALIGN_CENTER | wx.ALL )

  self.aBox = wx.StaticBox( self.aPanel, wx.ID_ANY, 'Foo' )
  self.aBoxSizer = wx.StaticBoxSizer( self.aBox, wx.VERTICAL )
  self.aBoxSizer.Add( self.aSizer, 0, wx.ALIGN_LEFT | wx.ALL )

  self.aPanel.SetSizer( self.aBoxSizer )
有帮助吗?

解决方案

I guess probably that's because you are using wx.StaticBoxSizer. If I replace it with wx.BoxSizer, the border parameter works as expected. Here's a minimal example. I don't know the exact layout that you want. So, it may not do what you actually want but I hope it helps as a starting point.

(edit: code modified according to comment)

import wx

class myframe(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, title='Test', size=(200,200))

        self.aPanel = wx.Panel(self, style = wx.RAISED_BORDER )
        self.aLabel = wx.StaticText( self.aPanel, -1, 'Bar' )
        self.aBox = wx.StaticBox(self.aPanel, -1, 'Foo')
        
        self.aSizer = wx.BoxSizer( wx.HORIZONTAL )
        self.aSizer.Add( self.aLabel, 0, wx.ALIGN_CENTER | wx.ALL )
        
        self.aBoxSizer = wx.StaticBoxSizer( self.aBox, wx.VERTICAL )
        self.aBoxSizer.Add( self.aSizer, 0, wx.ALIGN_LEFT | wx.ALL )
        
        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.aBoxSizer, 1, wx.EXPAND | wx.ALL, 20)
        self.aPanel.SetSizer( vbox )

        self.Show()


if __name__ == '__main__':

    app = wx.PySimpleApp(0)
    frame = myframe()
    app.MainLoop()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top