How to add elements inside a static field when using sizers?

I use wx.Python and have a group of objects that I want to "wrap" in a static field, similar to this:

enter image description here

However, the tutorial uses position sizes , and instead I use sizers. I find it hard to get elements inside:

enter image description here

but they are below the static field. How to include objects in a static cell using Sizers, and not in a position?

Here is 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) 
+6
source share
1 answer

When using Sizers, you must create a special β€œStatic Box”, which is Sizer, and contains the static box that you want to use. This is done by:

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

This means that your Static Box must be created in advance and is the argument that was passed to the Sizer. From there, the Sizer behaves exactly like a regular Sizer. This is what I got while correcting 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) 

What gives this result:

enter image description here

+11
source

Source: https://habr.com/ru/post/893832/


All Articles