GridBagSizer 实际上用于将多个小部件放入类似网格的形状中,例如当您有一个带有成对标签和文本控件的表单时。在大多数情况下,它不仅仅适用于一个小部件。
您应该使用 BoxSizer。无论如何,我发现它们更灵活。wx.PySimpleApp 也已弃用。请改用 wx.App(False)。这是一些有效的代码:
import wx
import wx.grid
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Manager")
sizer = wx.BoxSizer(wx.VERTICAL)
overall = wx.grid.Grid(self)
overall.CreateGrid(5,2)
sizer.Add(overall, 0, flag=wx.EXPAND)
self.SetSizer(sizer)
self.Fit()
app = wx.App(False)
MainFrame().Show()
app.MainLoop()
编辑(2012 年 7 月 16 日):这是另一个更符合您的描述的示例:
import wx
########################################################################
class ChartPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
########################################################################
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
chart = ChartPanel(self)
chart.SetBackgroundColour("blue")
# create some sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
topSizer = wx.BoxSizer(wx.HORIZONTAL)
# change to VERTICAL if the buttons need to be stacked
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
for i in range(3):
btn = wx.Button(self, label="Button #%s" % (i+1))
btnSizer.Add(btn, 0, wx.ALL, 5)
# put the buttons next to the Panel on the top
topSizer.Add(btnSizer, 0, wx.ALL, 5)
topSizer.Add(chart, 1, wx.EXPAND)
mainSizer.Add(topSizer, 1, wx.EXPAND)
mainSizer.AddSpacer(150,150)
self.SetSizer(mainSizer)
########################################################################
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="BoxSizer Example")
panel = MainPanel(self)
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
app.MainLoop()