wxPython - 嵌套sizer,面板和SetSizer / Fit的问题

时间:2011-07-09 14:19:39

标签: wxpython nested panel

我目前正试图抓住wxPython,我在嵌套测量器上做了标记,将它们放在面板上并适合它们。

我希望BoxSizer中的StaticBoxSizer中有一个GridBagSizer。整个应该放在一个小组上。

我的代码:

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)

    # ----

    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()

所以,上面的代码并没有真正起作用。但是,当我引用第10行和第11行,并且取消引用第13和14行时,至少正确显示了GridBagSizer。所以我怀疑我的SetSizer和Fit系列中的某些东西一定是错的......但是面板仍然卡在右上角并且不起作用......

任何暗示都很有用!

谢谢,Woodpicker

1 个答案:

答案 0 :(得分:0)

要在面板上显示的小部件应该是面板的子节点而不是框架。请尝试以下更改:

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))

如果您希望GridBagSizer与StaticBox一起展开,您仍然需要为GridBagSizer提供一个或多个可扩展列。