我正在使用wx.Python,并在类似于以下内容的静态框中包含一组要包装的对象:
但是那tutorial uses position sizes,而我使用的是大小调整器。我很难将物品放入里面:
但它们位于静态框下方。如何使用Sizers而不是位置将对象包括在静态框中?
这是我的代码:
# 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)
最佳答案
使用Sizers时,必须创建一个特定的“静态框缩放器”,该尺寸器是一个Sizer,并包含要使用的静态框。这是通过以下方式完成的:
self.foo = wx.StaticBoxSizer(self.box, wx.ORIENT)
这意味着您的静态框需要事先创建,这是传递给Sizer的争论。从此开始,Sizer的行为与常规Sizer完全相同。这是我修复您的代码的方法:
# 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)
产生以下结果: