wxPython:GridBagSizer:遍历 gridbagsizer 的行和列
wxPython: GridBagSizer: Iterating over rows and columns of a gridbagsizer
有没有办法以 row/column 顺序遍历 wx.GridBagSizer 的 Children?
我注意到我可以在 sizer 上执行 GetChildren() 但我在 API 中找不到说明这些 children 实际上如何映射到行的文档或列。
有两种方法:
GetEffectiveRowsCount()
GetEffectiveColsCount()
但我不确定如何使用这些遍历 children。
我在下面包含了一个模板:
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()
返回的项目只是按添加顺序添加到 sizer 的项目的集合。在 Classic 中,项目是 wx.SizerItem
个对象,因此您无法从中获取有关位置或跨度的任何信息。但是在 Phoenix 中它们是 wx.GBSizerItem
对象,因此您可以根据需要使用 GetPos
来查找它们在网格中的位置。
要按网格顺序遍历项目,您可以使用 FindItemAtPosition
which returns a wx.GBSizerItem
或 None
如果在请求的位置没有项目。所以像这样的东西应该给你你正在寻找的东西:
for row in gridSizer.GetEffectiveRowsCount():
for col in gridSizer.GetEffectiveColsCount():
item = gridSizer.FindItemAtPosition((row, col))
if item is not None:
print(item.GetSize())
有没有办法以 row/column 顺序遍历 wx.GridBagSizer 的 Children?
我注意到我可以在 sizer 上执行 GetChildren() 但我在 API 中找不到说明这些 children 实际上如何映射到行的文档或列。 有两种方法:
GetEffectiveRowsCount()
GetEffectiveColsCount()
但我不确定如何使用这些遍历 children。 我在下面包含了一个模板:
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()
返回的项目只是按添加顺序添加到 sizer 的项目的集合。在 Classic 中,项目是 wx.SizerItem
个对象,因此您无法从中获取有关位置或跨度的任何信息。但是在 Phoenix 中它们是 wx.GBSizerItem
对象,因此您可以根据需要使用 GetPos
来查找它们在网格中的位置。
要按网格顺序遍历项目,您可以使用 FindItemAtPosition
which returns a wx.GBSizerItem
或 None
如果在请求的位置没有项目。所以像这样的东西应该给你你正在寻找的东西:
for row in gridSizer.GetEffectiveRowsCount():
for col in gridSizer.GetEffectiveColsCount():
item = gridSizer.FindItemAtPosition((row, col))
if item is not None:
print(item.GetSize())