使用wxPython将sizer嵌套在一个框中

时间:2013-10-01 06:03:13

标签: wxpython sizer

我希望制作水平的标签和文本字段行,并将行放在带有标签的框中,以便将它们很好地分组。但是,由于某种原因,该框仅延伸到系列的最后一行(见下图)我希望它将所有行分组。

是否有更简单的方法来制作这样的分组和标记界面?

Error with box label

生成上述代码的代码,

import wx

class ExamplePanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self.mainBox = wx.StaticBox(self, id=-1, label="Box Label")
        self.mainSizer = wx.StaticBoxSizer(self.mainBox, wx.VERTICAL)

        labels = ["A: ", "B: ", "C: "]
        for i, label in enumerate(labels):
            itemLabel = wx.StaticText(self, label=label)
            itemTextCtrl = wx.TextCtrl(self, value="placeholder")
            itemTextCtrl.SetForegroundColour(wx.LIGHT_GREY)
            rowSizer = wx.StaticBoxSizer(self.mainBox, wx.HORIZONTAL)
            rowSizer.Add(itemLabel, 0, wx.ALL, 0)
            rowSizer.Add(itemTextCtrl, 0, wx.ALL, 0)
            self.mainSizer.Add(rowSizer)

        self.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self)

if __name__ == '__main__':
    app = wx.App(False)
    frame = wx.Frame(None)
    panel = ExamplePanel(frame)
    frame.Show()
    app.MainLoop()

2 个答案:

答案 0 :(得分:1)

问题是您不想嵌套StaticBoxSizer。相反,只需为您的rowSizer使用普通的BoxSizer:

import wx

class ExamplePanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self.mainBox = wx.StaticBox(self, id=-1, label="Box Label")
        self.mainSizer = wx.StaticBoxSizer(self.mainBox, wx.VERTICAL)

        labels = ["A: ", "B: ", "C: "]
        for i, label in enumerate(labels):
            itemLabel = wx.StaticText(self, label=label)
            itemTextCtrl = wx.TextCtrl(self, value="placeholder")
            itemTextCtrl.SetForegroundColour(wx.LIGHT_GREY)
            rowSizer = wx.BoxSizer(wx.HORIZONTAL)
            rowSizer.Add(itemLabel, 0, wx.ALL, 0)
            rowSizer.Add(itemTextCtrl, 0, wx.ALL, 0)
            self.mainSizer.Add(rowSizer)

        self.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self)

if __name__ == '__main__':
    app = wx.App(False)
    frame = wx.Frame(None)
    panel = ExamplePanel(frame)
    frame.Show()
    app.MainLoop()

请注意,小部件周围的框不紧。如果它是主要的sizer,我认为不可能。

答案 1 :(得分:1)

你的意思是这样吗

导入wx

class ExamplePanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self.mainBox = wx.StaticBox(self, label="Box Label")
        self.boxSizer = wx.StaticBoxSizer(self.mainBox, wx.VERTICAL)

        labels = ["A: ", "B: ", "C: "]
        for label in labels:
            itemLabel = wx.StaticText(self, label=label)
            itemTextCtrl = wx.TextCtrl(self, value="placeholder")
            itemTextCtrl.SetForegroundColour(wx.LIGHT_GREY)
            rowSizer = wx.BoxSizer(wx.HORIZONTAL)
            rowSizer.Add(itemLabel, 0, wx.ALL, 2)
            rowSizer.Add(itemTextCtrl, 0, wx.ALL, 2)
            self.boxSizer.Add(rowSizer)

        pSizer = wx.BoxSizer(wx.VERTICAL)
        pSizer.Add(self.boxSizer, 0, wx.ALL, 7)
        self.SetSizer(pSizer)

if __name__ == '__main__':
    app = wx.App(False)
    frame = wx.Frame(None)
    panel = ExamplePanel(frame)
    frame.Show()
    app.MainLoop()