Search code examples
textbuttonwxpythonbordersizer

How do I add items inside a static box while using Sizers?


I'm using wx.Python and have a group of objects that I want 'wrapped' within a static box similar to this:

enter image description here

However that tutorial uses position sizes, and I'm using sizers instead. I'm having a hard time getting the items inside:

enter image description here

but rather they're below the static box. How do I include the objects within the static box using Sizers and not position?

Here's my code:

    # Date and Graph Type Selection 

    self.dateLbl = wx.StaticBox(self, -1, 'Date Range:', size=(240, 140))
    self.dategraphSizer = wx.BoxSizer(wx.VERTICAL)
    self.dategraphSizer.Add(self.dateLbl, 0, wx.ALL|wx.LEFT, 5)

    # Date Range Selection
    self.dateSizer = wx.BoxSizer(wx.HORIZONTAL)
    self.dateone = wx.TextCtrl(self, -1, style=wx.ALIGN_LEFT)
    self.datetwo = wx.TextCtrl(self, -1, style=wx.ALIGN_LEFT)
    self.date2Lbl = wx.StaticText(self, -1, "TO")
    self.dateSizer.Add(self.dateone, 0, wx.ALL|wx.CENTER, 2)
    self.dateSizer.Add(self.date2Lbl, 0, wx.ALL|wx.CENTER, 2)
    self.dateSizer.Add(self.datetwo, 0, wx.ALL|wx.CENTER, 2)


    # Date Quick Selection Buttons
    self.dategraphSizer.Add(self.dateSizer, 0, wx.ALL|wx.CENTER, 5)
    self.todayButton = wx.Button(self, -1, 'Today Only')
    self.dategraphSizer.Add(self.todayButton, 0, wx.ALL|wx.LEFT, 5)
    self.recentButton = wx.Button(self, -1, 'Most Recent Session')
    self.dategraphSizer.Add(self.recentButton, 0, wx.ALL|wx.LEFT, 5)

Solution

  • When using Sizers, you have to create a specific 'Static Box Sizer' that is a Sizer and contains the Static Box you want to use. This is done by:

    self.foo = wx.StaticBoxSizer(self.box, wx.ORIENT)
    

    This means that your Static Box needs to be created beforehand and is an arguement passed to the creation of the Sizer. From there on, the Sizer behaves exactly like a regular Sizer. This is what I got fixing your code:

        # Date and Graph Type Selection 
    
        self.dateLbl = wx.StaticBox(self, -1, 'Date Range:', size=(240, 140))
        self.dategraphSizer = wx.StaticBoxSizer(self.dateLbl, wx.VERTICAL)
        #self.dategraphSizer.Add(self.dateLbl, 0, wx.ALL|wx.LEFT, 5) NOTE THIS ISN'T NEEDED ANYMORE
    
        # Date Range Selection
        self.dateSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.dateone = wx.TextCtrl(self, -1, style=wx.ALIGN_LEFT)
        self.datetwo = wx.TextCtrl(self, -1, style=wx.ALIGN_LEFT)
        self.date2Lbl = wx.StaticText(self, -1, "TO")
        self.dateSizer.Add(self.dateone, 0, wx.ALL|wx.CENTER, 2)
        self.dateSizer.Add(self.date2Lbl, 0, wx.ALL|wx.CENTER, 2)
        self.dateSizer.Add(self.datetwo, 0, wx.ALL|wx.CENTER, 2)
    
    
        # Date Quick Selection Buttons
        self.dategraphSizer.Add(self.dateSizer, 0, wx.ALL|wx.CENTER, 5)
        self.todayButton = wx.Button(self, -1, 'Today Only')
        self.dategraphSizer.Add(self.todayButton, 0, wx.ALL|wx.LEFT, 5)
        self.recentButton = wx.Button(self, -1, 'Most Recent Session')
        self.dategraphSizer.Add(self.recentButton, 0, wx.ALL|wx.LEFT, 5)
    

    Which yields this result:

    enter image description here