0

I wish to make horizontal rows of labels and text fields, and place the rows inside a box with a label so they are grouped nicely. However, for some reason the box only extends around the last row in the series (see image below) I would like it to group all of the rows.

Is there an easier way to make a grouped and labelled interface like this?

Error with box label

The code to generate the above,

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

2 回答 2

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

请注意,小部件周围的盒子并不紧。如果它是主要的尺寸器,我认为它不可能。

于 2013-10-01T13:34:27.257 回答
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()
于 2013-10-01T13:44:17.417 回答