何时使用水平和垂直BoxSizer?
当你想要你的元素垂直放置在彼此那么你使用垂直boxsizer。
当你想要你的元素水平放置到彼此那么你使用水平boxsizer。
见下面这张图片,这是很容易理解:
示例代码:
我创建垂直boxsizer在myPanelA
和myPanelB
水平boxsizer。你可以在上面的图片中看到这些元素如何相互对齐的区别?
import wx
class test(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self,None, id, title, style=wx.DEFAULT_FRAME_STYLE)
self.myPanelA = wx.Panel(self, -1, size=(200,200), pos=(0,0))
self.myPanelB = wx.Panel(self, -1, size=(200,200), pos=(0,205))
self.mytextA = wx.StaticText(self.myPanelA, -1, 'My-PanelA', size=(80,20))
self.myPanelA.SetBackgroundColour('#F57373')
self.mytextB = wx.StaticText(self.myPanelB, -1, 'My-PanelB', size=(80,20))
self.myPanelB.SetBackgroundColour('#73F573')
self.mySizerA = wx.BoxSizer(wx.VERTICAL)
self.mySizerB = wx.BoxSizer(wx.HORIZONTAL)
self.button1 = wx.Button(self.myPanelA, -1, size=(18,18))
self.button2 = wx.Button(self.myPanelA, -1, size=(18,18))
self.button3 = wx.Button(self.myPanelA, -1, size=(18,18))
self.button4 = wx.Button(self.myPanelB, -1, size=(18,18))
self.button5 = wx.Button(self.myPanelB, -1, size=(18,18))
self.button6 = wx.Button(self.myPanelB, -1, size=(18,18))
self.mySizerA.Add(self.mytextA, 0, wx.ALL, 5)
self.mySizerA.Add(self.button1, 0, wx.ALL, 5)
self.mySizerA.Add(self.button2, 0, wx.ALL, 5)
self.mySizerA.Add(self.button3, 0, wx.ALL, 5)
self.myPanelA.SetSizer(self.mySizerA)
self.myPanelA.Layout()
self.mySizerB.Add(self.mytextB, 0, wx.ALL, 5)
self.mySizerB.Add(self.button4, 0, wx.ALL, 5)
self.mySizerB.Add(self.button5, 0, wx.ALL, 5)
self.mySizerB.Add(self.button6, 0, wx.ALL, 5)
self.myPanelB.SetSizer(self.mySizerB)
self.myPanelB.Layout()
if __name__ == "__main__":
app = wx.App()
frame = test(parent=None, id=-1, title="Sizer demo")
frame.Show()
app.MainLoop()
如果更改此行self.mySizerA.Add(self.button1, 0, wx.ALL, 5)
到
self.mySizerA.Add(self.button1, 1, wx.ALL, 5)
你会发现button1
比其他按钮大如下面的图所示。 所以,这是比例。
此代码行中的5
表示您希望所有组件之间的空间距离为5
。试着让它10
看看会发生什么?
我希望它很清楚。我会建议你玩这个代码片段。
感谢您的简单解释。但你仍然没有回答关于我发布的代码。看看代码,这是我的问题的细节。 –