wxpython:如何访问您所访问的对象';你把它放在一个wx.Sizer对象里面了吗?
在重构会话期间,我将大量gui代码放入一些小函数中,这些函数直接将数据插入到wx.Sizer对象中 例如,这个小功能:wxpython:如何访问您所访问的对象';你把它放在一个wx.Sizer对象里面了吗?,python,wxpython,Python,Wxpython,在重构会话期间,我将大量gui代码放入一些小函数中,这些函数直接将数据插入到wx.Sizer对象中 例如,这个小功能: def buildStatusDisplay(self, status_disp, flag): grid = wx.FlexGridSizer(cols=1, hgap=5, vgap=0) font = wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.LIGHT, False, 'Segoe UI') for option
def buildStatusDisplay(self, status_disp, flag):
grid = wx.FlexGridSizer(cols=1, hgap=5, vgap=0)
font = wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.LIGHT, False, 'Segoe UI')
for option in status_disp:
left = wx.StaticText(self, -1, option)
left.SetFont(font)
grid.Add(left, 0, flag=flag)
return grid
因此,它使用一次性left
创建所有StaticText对象,然后返回FlexGrid
。但是,我需要根据用户输入更新StaticText对象中的一些标签,但是,我不确定如何访问它们
我已经遍历了从函数返回的sizer,但是我没有看到任何与让我获得对组成sizer的对象的引用有关的方法
for i in dir(sizer):
print i
>>Add
>>AddF
>>AddGrowableCol
>>AddGrowableRow
>>AddItem
>>AddMany
>>AddSizer
>>AddSpacer
>>AddStretchSpacer
>>AddWindow
>>CalcMin
>>CalcRowsCols
>>Children
>>ClassName
>>Clear
>>ColWidths
>>Cols
>>ComputeFittingCli
>>ComputeFittingWin
>>ContainingWindow
>>DeleteWindows
>>Destroy
>>Detach
>>Fit
>>FitInside
>>FlexibleDirection
>>GetChildren
>>GetClassName
>>GetColWidths
>>GetCols
>>GetContainingWind
>>GetFlexibleDirect
>>GetHGap
>>GetItem
>>GetItemIndex
>>GetMinSize
>>GetMinSizeTuple
>>GetNonFlexibleGro
>>GetPosition
>>GetPositionTuple
>>GetRowHeights
>>GetRows
>>GetSize
>>GetSizeTuple
>>GetVGap
>>HGap
>>Hide
>>Insert
>>InsertF
>>InsertItem
>>InsertSizer
>>InsertSpacer
>>InsertStretchSpac
>>InsertWindow
>>IsSameAs
>>IsShown
>>Layout
>>MinSize
>>NonFlexibleGrowMo
>>Position
>>Prepend
>>PrependF
>>PrependItem
>>PrependSizer
>>PrependSpacer
>>PrependStretchSpa
>>PrependWindow
>>RecalcSizes
>>Remove
>>RemoveGrowableCol
>>RemoveGrowableRow
>>RemovePos
>>RemoveSizer
>>RemoveWindow
>>Replace
>>RowHeights
>>Rows
>>SetCols
>>SetContainingWind
>>SetDimension
>>SetFlexibleDirect
>>SetHGap
>>SetItemMinSize
>>SetMinSize
>>SetNonFlexibleGro
>>SetRows
>>SetSizeHints
>>SetVGap
>>SetVirtualSizeHin
>>Show
>>ShowItems
>>Size
>>VGap
>>_ReplaceItem
>>_ReplaceSizer
>>_ReplaceWin
>>_SetItemMinSize
>>__class__
>>__del__
>>__delattr__
>>__dict__
>>__doc__
>>__format__
>>__getattribute__
>>__hash__
>>__init__
>>__module__
>>__new__
>>__reduce__
>>__reduce_ex__
>>__repr__
>>__setattr__
>>__sizeof__
>>__str__
>>__subclasshook__
>>__swig_destroy__
>>__weakref__
>>_setOORInfo
GetChildren()
只返回SizerItems
,它似乎也不包含对底层对象的任何引用
有人知道如何进入施胶器内部吗?我想我可以稍微分解一下函数,然后返回一个对象列表和sizer,或者简单地转储函数并将标识符保留在main中需要修改的对象上。。但是我的gui代码已经够意大利面了。如果我能避免,我愿意 在
wx.SizerItem
上,有一个GetWindow
,它提供了确切的信息。使用IsWindow
检查是否有可用的“窗口”。所有底层对象都继承自wx.Window
,所以这就是您想要的
下面是一个示例,obj
是一个wx.Frame
对象:
>>> obj
<main.views.Main; proxy of <Swig Object of type 'wxFrame *' at 0x7fbaa1c70820> >
>>> obj.Sizer
<wx._core.BoxSizer; proxy of <Swig Object of type 'wxBoxSizer *' at 0x7fba9c9d48d0> >
>>> obj.Sizer.Children
wxSizerItemList: [<wx._core.SizerItem; proxy of <Swig Object of type 'wxSizerItem *' at 0x7fbaa1c21630> >, <wx._core.SizerItem; proxy of <Swig Object of type 'wxSizerItem *' at 0x7fbaa1c7faf0> >]
>>> obj.Sizer.Children[0]
<wx._core.SizerItem; proxy of <Swig Object of type 'wxSizerItem *' at 0x7fbaa1c21630> >
>>> obj.Sizer.Children[0].Window
<wx._windows.Panel; proxy of <Swig Object of type 'wxPanel *' at 0x7fba9c9d4d10> >
>>obj
>>>物体尺寸测量仪
>>>儿童
wxSizerItemList:[,]
>>>对象Sizer.Children[0]
>>>obj.Sizer.Children[0]。窗口
在wx.SizerItem
上,有一个GetWindow
,它提供了确切的信息。使用IsWindow
检查是否有可用的“窗口”。所有底层对象都继承自wx.Window
,所以这就是您想要的
下面是一个示例,obj
是一个wx.Frame
对象:
>>> obj
<main.views.Main; proxy of <Swig Object of type 'wxFrame *' at 0x7fbaa1c70820> >
>>> obj.Sizer
<wx._core.BoxSizer; proxy of <Swig Object of type 'wxBoxSizer *' at 0x7fba9c9d48d0> >
>>> obj.Sizer.Children
wxSizerItemList: [<wx._core.SizerItem; proxy of <Swig Object of type 'wxSizerItem *' at 0x7fbaa1c21630> >, <wx._core.SizerItem; proxy of <Swig Object of type 'wxSizerItem *' at 0x7fbaa1c7faf0> >]
>>> obj.Sizer.Children[0]
<wx._core.SizerItem; proxy of <Swig Object of type 'wxSizerItem *' at 0x7fbaa1c21630> >
>>> obj.Sizer.Children[0].Window
<wx._windows.Panel; proxy of <Swig Object of type 'wxPanel *' at 0x7fba9c9d4d10> >
>>obj
>>>物体尺寸测量仪
>>>儿童
wxSizerItemList:[,]
>>>对象Sizer.Children[0]
>>>obj.Sizer.Children[0]。窗口
事实上,我在几个月前就回答了这类问题:
基本上,您只需要做如下操作:
children = self.sizer.GetChildren()
for child in children:
widget = child.GetWindow()
print widget
if isinstance(widget, wx.TextCtrl):
widget.Clear()
在本例中,您可以看到我只是在检查wx.TextCtrl,但您可以检查您喜欢的任何小部件。我也在我的博客上写了一篇关于这个主题的文章。几个月前,我实际上回答了这样一个问题: 基本上,您只需要做如下操作:
children = self.sizer.GetChildren()
for child in children:
widget = child.GetWindow()
print widget
if isinstance(widget, wx.TextCtrl):
widget.Clear()
在本例中,您可以看到我只是在检查wx.TextCtrl,但您可以检查您喜欢的任何小部件。我也在我的网站上写了一篇关于这个主题的文章。可能已经在其他地方引用过,但找不到,所以把它放在这里 在子项创建过程中,还可以使用
name
参数执行此操作,即:
left = wx.StaticText(self, -1, option, name='StaticTextChild')
然后可以直接引用/更改子对象,而无需Sizer引用
staticTextChild = wx.FindWindowByName('StaticTextChild')
staticTextChild.SetLabel('new label:')
在本例中,此方法可能无法直接工作,因为您需要确保唯一的子名称才能访问它们。您将如何做取决于您。可能已经在其他地方引用过,但找不到它,所以将其放在这里 在子项创建过程中,还可以使用
name
参数执行此操作,即:
left = wx.StaticText(self, -1, option, name='StaticTextChild')
然后可以直接引用/更改子对象,而无需Sizer引用
staticTextChild = wx.FindWindowByName('StaticTextChild')
staticTextChild.SetLabel('new label:')
在本例中,此方法可能无法直接工作,因为您需要确保唯一的子名称才能访问它们。你会怎么做取决于你。不幸的是,运气不好。我尝试了
IsWindow
,以及GetWindow
。两个都是错误的。啊,你完全正确。谢谢你的编辑。我最初是在主sizer上调用窗口
,没有意识到我需要在实际的sizer项目上这样做。谢谢不幸的是没有运气。我尝试了IsWindow
,以及GetWindow
。两个都是错误的。啊,你完全正确。谢谢你的编辑。我最初是在主sizer上调用窗口
,没有意识到我需要在实际的sizer项目上这样做。谢谢不推荐使用wx.FindWindowByName。改用wx.Window.FindWindowByName。另外,当名称不唯一时会发生什么情况?FindWindowByName方法将在找到第一个匹配项时立即停止,并且您将无法访问任何可能的后续匹配项。至少我没有意识到这种可能性,但我不是wxpyhton专家…wx.FindWindowByName已被弃用。改用wx.Window.FindWindowByName。另外,当名称不唯一时会发生什么情况?FindWindowByName方法将在找到第一个匹配项时立即停止,并且您将无法访问任何可能的后续匹配项。至少我没有意识到这种可能性,但我不是wxpyhton的专家。。。