wxPython:GridBagSizer:遍历网格袋的行和列



有没有办法遍历wx的子项。按行/列顺序的网格袋大小器?

我注意到我可以在大小器上执行 GetChildren(),但我在 API 中找不到说明这些孩子如何实际映射到行或列的文档。有两种方法:

GetEffectiveRowsCount()
GetEffectiveColsCount()

但我不确定如何使用这些来遍历孩子。我在下面包含一个模板:

import wx
import wx.lib.inspection

class MyRegion(wx.Frame):
    I = 0
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="My Region")
        gridSizer = wx.GridBagSizer()
        gridSizer.Add(self.CreateStaticBoxSizer(4), pos=(0,0), span=(4,1), flag=wx.EXPAND)
        gridSizer.Add(self.CreateStaticBoxSizer(4), pos=(4,0), span=(4,1), flag=wx.EXPAND)
        gridSizer.Add(self.CreateStaticBoxSizer(4), pos=(0,1), span=(4,1), flag=wx.EXPAND)
        gridSizer.Add(self.CreateStaticBoxSizer(8), pos=(0,2), span=(7,1), flag=wx.EXPAND)
        gridSizer.Add(self.CreateStaticBoxSizer(6), pos=(0,3), span=(6,1), flag=wx.EXPAND)
        self.SetSizer(gridSizer)
        for item in gridSizer.Children:
            for r in range(0, gridSizer.GetEffectiveRowsCount()):
                print(item.GetSize())
    def CreateStaticBoxSizer(self, x=4):
        box = wx.StaticBox(parent=self, label="StaticBox::" + str(MyRegion.I))
        MyRegion.I += 1
        sz = wx.StaticBoxSizer(orient=wx.VERTICAL, box=box)
        for i in range(x):
            sz.Add(wx.StaticText(parent=sz.GetStaticBox(),
                                 label="This window is a child of the staticbox"))
        return sz

if __name__ == "__main__":
    app = wx.App()
    frame = MyRegion(None)
    frame.Show()
    wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()

GetChildren()返回的项只是按添加顺序添加到大小器的项的集合。 在经典中,项目是wx.SizerItem对象,因此您无法从中获取有关位置或跨越的任何信息。 但是,在 Phoenix 中,它们是wx.GBSizerItem对象,因此如果需要,您可以使用GetPos在网格中查找它们的位置。

要按网格顺序循环访问项目,您可以使用 FindItemAtPosition 如果请求的位置没有项目,则返回wx.GBSizerItemNone。 所以像这样的东西应该给你你想要的东西:

for row in gridSizer.GetEffectiveRowsCount():
    for col in gridSizer.GetEffectiveColsCount():
        item = gridSizer.FindItemAtPosition((row, col))
        if item is not None:
            print(item.GetSize())

最新更新