Search code examples
user-interfacewxpython

WX StaticBoxSizer/Sizer border width


I have made a small example of what I want to achieve which is a StaticBoxSizer which has a fixed border width which can be set. The following example shows several staticbox objects but, how do you adjust their border width/thickness?

Can any other sizer do the same job? i.e. provide borders of adjustable width around their children?

import wx

class mypanel(wx.Panel):
    def __init__ (self, parent):
        wx.Panel.__init__(self, parent, size=(-1, -1))

        mysizer4 = wx.BoxSizer(orient=wx.HORIZONTAL)
        box1 = wx.StaticBox(self, size=(-1,-1), style=wx.SUNKEN_BORDER)
        box2 = wx.StaticBox(self, size=(-1,-1), style=wx.SUNKEN_BORDER)
        box3 = wx.StaticBox(self, size=(-1,-1), style=wx.SUNKEN_BORDER)

        mysizer   = wx.StaticBoxSizer(box1, orient=wx.HORIZONTAL)
        mysizer2  = wx.StaticBoxSizer(box2, orient=wx.VERTICAL)
        mysizer3  = wx.StaticBoxSizer(box3, orient=wx.HORIZONTAL)

        mybutton = wx.Button(self, size=(-1,-1), label="hello")
        mybutton2 = wx.Button(self, size=(-1,-1), label="hello")
        mybutton3 = wx.Button(self, size=(-1,-1), label="hello")

        mytext1 = wx.StaticText(self, label="hello")
        mytext2 = wx.StaticText(self, label="bye")

        mytext3 = wx.StaticText(self, label="hello")
        mytext4 = wx.StaticText(self, label="bye")

        mytext5 = wx.StaticText(self, label="hello")
        mytext6 = wx.StaticText(self, label="bye")

        mysizer.Add(mybutton,      flag=wx.EXPAND)
        mysizer.Add(mytext1,      flag=wx.EXPAND)
        mysizer.Add(mytext2,      flag=wx.EXPAND)

        mysizer2.Add(mybutton2,    flag=wx.EXPAND)
        mysizer2.Add(mytext3,    flag=wx.EXPAND)
        mysizer2.Add(mytext4,    flag=wx.EXPAND)

        mysizer3.Add(mybutton3,    flag=wx.EXPAND)
        mysizer3.Add(mytext5,    flag=wx.EXPAND)
        mysizer3.Add(mytext6,    flag=wx.EXPAND)

        mysizer4.Add(mysizer)
        mysizer4.Add(mysizer2)
        mysizer4.Add(mysizer3)
        self.SetSizer(mysizer4)

class myframe(wx.Frame):
    def __init__(self):
        "Constructor. No arguments"
        wx.Frame.__init__(self, None, size=(-1,-1))
        sizer = wx.BoxSizer(orient=wx.HORIZONTAL)
        mydiag = mypanel(self)
        sizer.Add(mydiag)
        self.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    region = myframe()
    region.Show()
    app.MainLoop()

Solution

  • Sizers don't really support displaying their borders. You happened to find the only one that did, but as far as I know, the configuration options for that widget are pretty limited. I've seen an example of border highlighting done in the Widget Inspection Tool, which is included with wxPython.

    You can read about it on the wiki. You should take a look at its source to figure out how it's done.

    Or you might find the following thread useful:

    It gives a workaround wherein you use panels with background colours instead of sizers.