例子
在下面的示例中,创建了两个静态框尺寸器并将其添加到顶部垂直框尺寸器中,该尺寸器控制框架内面板的布局。
第一个 staticbox sizer 是围绕一个名为“Name”的静态框创建的。
nm = wx.StaticBox(panel, -1, 'Name:')
nmSizer = wx.StaticBoxSizer(nm, wx.VERTICAL)
一个Horizontal box sizer,包含两个标签和两个文本框,被添加到nmSizer静态box sizer中。
nmbox = wx.BoxSizer(wx.HORIZONTAL)
fn = wx.StaticText(panel, -1, "First Name")
nmbox.Add(fn, 0, wx.ALL|wx.CENTER, 5)
nm1 = wx.TextCtrl(panel, -1, style = wx.ALIGN_LEFT)
nm2 = wx.TextCtrl(panel, -1, style = wx.ALIGN_LEFT)
ln = wx.StaticText(panel, -1, "Last Name")
nmbox.Add(nm1, 0, wx.ALL|wx.CENTER, 5)
nmbox.Add(ln, 0, wx.ALL|wx.CENTER, 5)
nmbox.Add(nm2, 0, wx.ALL|wx.CENTER, 5)
nmSizer.Add(nmbox, 0, wx.ALL|wx.CENTER, 10)
同样,另一个 staticbox sizer 拥有一个名为“Buttons”的静态框。
sbox = wx.StaticBox(panel, -1, 'buttons:')
sboxSizer = wx.StaticBoxSizer(sbox, wx.VERTICAL)
名为“ok”和“cancel”的两个按钮对象被放入一个水平 box sizer 中,而后者又被放置在第二个 staticbox sizer 中。
hbox = wx.BoxSizer(wx.HORIZONTAL)
okButton = wx.Button(panel, -1, 'ok')
hbox.Add(okButton, 0, wx.ALL|wx.LEFT, 10)
cancelButton = wx.Button(panel, -1, 'cancel')
hbox.Add(cancelButton, 0, wx.ALL|wx.LEFT, 10)
sboxSizer.Add(hbox, 0, wx.ALL|wx.LEFT, 10)
两个静态盒子尺寸器,“名称”和“按钮”被添加到一个垂直盒子尺寸器中,作为顶层框架中面板的布局管理器。
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(nmSizer,0, wx.ALL|wx.CENTER, 5)
vbox.Add(sboxSizer,0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(vbox)
以下是完整的代码 -
import wx
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title)
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
nm = wx.StaticBox(panel, -1, 'Name:')
nmSizer = wx.StaticBoxSizer(nm, wx.VERTICAL)
nmbox = wx.BoxSizer(wx.HORIZONTAL)
fn = wx.StaticText(panel, -1, "First Name")
nmbox.Add(fn, 0, wx.ALL|wx.CENTER, 5)
nm1 = wx.TextCtrl(panel, -1, style = wx.ALIGN_LEFT)
nm2 = wx.TextCtrl(panel, -1, style = wx.ALIGN_LEFT)
ln = wx.StaticText(panel, -1, "Last Name")
nmbox.Add(nm1, 0, wx.ALL|wx.CENTER, 5)
nmbox.Add(ln, 0, wx.ALL|wx.CENTER, 5)
nmbox.Add(nm2, 0, wx.ALL|wx.CENTER, 5)
nmSizer.Add(nmbox, 0, wx.ALL|wx.CENTER, 10)
sbox = wx.StaticBox(panel, -1, 'buttons:')
sboxSizer = wx.StaticBoxSizer(sbox, wx.VERTICAL)
hbox = wx.BoxSizer(wx.HORIZONTAL)
okButton = wx.Button(panel, -1, 'ok')
hbox.Add(okButton, 0, wx.ALL|wx.LEFT, 10)
cancelButton = wx.Button(panel, -1, 'cancel')
hbox.Add(cancelButton, 0, wx.ALL|wx.LEFT, 10)
sboxSizer.Add(hbox, 0, wx.ALL|wx.LEFT, 10)
vbox.Add(nmSizer,0, wx.ALL|wx.CENTER, 5)
vbox.Add(sboxSizer,0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(vbox)
self.Centre()
panel.Fit()
self.Show()
app = wx.App()
Mywin(None, 'staticboxsizer demo')
app.MainLoop()
上面的代码产生以下输出 -