I am currently trying to get a grip on wxPython, and I stumbeled over nesting sizers, putting them on a panel and fit them.
I want to have a GridBagSizer in a StaticBoxSizer in a BoxSizer. The whole should be on a panel.
My code:
import wx
class App(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Frame", size=(550, 300))
panel = wx.Panel(self, -1)
mainSizer = self.makeOuterSizer()
# fit layout
panel.SetSizer(mainSizer)
mainSizer.Fit(panel)
## mainSizer.Fit(self)
## self.SetSizer(mainSizer)
# 开发者_JAVA技巧----
def makeOuterSizer(self):
# define sizer
innerSizer = self.makeGrid()
# make
outerSizer = wx.BoxSizer(wx.VERTICAL)
outerSizer.Add(innerSizer, 0, wx.EXPAND|wx.CENTER|wx.ALL, 10)
return outerSizer
# ----
def makeGrid(self):
sizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, ""), wx.VERTICAL)
# make fields
itemMonomer1_label = wx.StaticText(self, -1, "Text 1")
self.itemMonomer1_value = wx.TextCtrl(self, -1, "", size=(100, -1))
itemMonomer2_label = wx.StaticText(self, -1, "Text 2")
self.itemMonomer2_value = wx.TextCtrl(self, -1, "", size=(100, -1))
itemMonomer3_label = wx.StaticText(self, -1, "Text 3")
self.itemMonomer3_value = wx.TextCtrl(self, -1, "", size=(100, -1))
# pack elements
grid = wx.GridBagSizer(2, 3)
grid.Add(itemMonomer1_label, (0,0), flag=wx.ALIGN_CENTER|wx.ALIGN_CENTER_VERTICAL)
grid.Add(self.itemMonomer1_value, (1,0), flag=wx.EXPAND)
grid.Add(itemMonomer2_label, (0,1), flag=wx.ALIGN_CENTER|wx.ALIGN_CENTER_VERTICAL)
grid.Add(self.itemMonomer2_value, (1,1), flag=wx.EXPAND)
grid.Add(itemMonomer3_label, (0,2), flag=wx.ALIGN_CENTER|wx.ALIGN_CENTER_VERTICAL)
grid.Add(self.itemMonomer3_value, (1,2), flag=wx.EXPAND)
sizer.Add(grid, 0, wx.EXPAND|wx.ALIGN_CENTER|wx.ALL, 5)
return sizer
# ----
if __name__ == '__main__':
app = wx.App()
App().Show(True)
app.MainLoop()
So, the code above does not really work. However, when I quote lines 10 and 11, and unquote lines 13 and 14, at least the GridBagSizer is correctly shown. So I suspect that something in my SetSizer and Fit lines must be wrong... But the panel is still stuck in the upper right corner and does not work...
Any hint is much apprechiated!
Thanks, Woodpicker
The widgets you want to display on the panel should be children of the panel instead of the frame. Try the following changes:
class App(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Frame", size=(550, 300))
self.panel = wx.Panel(self, -1)
mainSizer = self.makeOuterSizer()
# fit layout
self.panel.SetSizer(mainSizer)
mainSizer.Fit(self.panel)
# ...
def makeGrid(self):
sizer = wx.StaticBoxSizer(wx.StaticBox(self.panel, -1, ""), wx.VERTICAL)
# make fields
itemMonomer1_label = wx.StaticText(self.panel, -1, "Text 1")
self.itemMonomer1_value = wx.TextCtrl(self.panel, -1, "", size=(100, -1))
itemMonomer2_label = wx.StaticText(self.panel, -1, "Text 2")
self.itemMonomer2_value = wx.TextCtrl(self.panel, -1, "", size=(100, -1))
itemMonomer3_label = wx.StaticText(self.panel, -1, "Text 3")
self.itemMonomer3_value = wx.TextCtrl(self.panel, -1, "", size=(100, -1))
You will still have to give the GridBagSizer one or more growable columns if you want it to expand along with the StaticBox.
精彩评论