wxpython:如何访问放置在wx.Sizer对象内部的对象?

问题描述 投票:2回答:3

在重构会话期间,我将很多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 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中的内容吗?我想我可以稍微炸开该函数,然后返回对象列表以及sizer,或者简单转储该函数并将标识符保留为需要在main中修改的对象..但是...我的gui代码已经足够了意大利面。如果我能避免的话,我想。

python wxpython
3个回答
1
投票

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

1
投票

几个月前我实际上回答了这种问题:wxPython: How to get sizer from wx.StaticText?

基本上,您只需要执行以下操作:

children = self.sizer.GetChildren()

for child in children:
    widget = child.GetWindow()
    print widget
    if isinstance(widget, wx.TextCtrl):
        widget.Clear()

在这种情况下,您可以看到我只是在检查wx.TextCtrl,但是您可以检查所需的任何小部件。我还在blog上写了一篇有关该主题的文章。


1
投票

可能已经在其他地方引用了,但是找不到它,所以放在这里...

[在创建子代时,还有使用name参数的另一种方法,即:

left = wx.StaticText(self, -1, option, name='StaticTextChild')

然后可以直接引用/更改子级,而无需Sizer引用。

staticTextChild = wx.FindWindowByName('StaticTextChild')
staticTextChild.SetLabel('new label:')

此方法在本示例中可能无法直接使用,因为您需要确保唯一的子名称能够访问它们。您将如何操作取决于您。

© www.soinside.com 2019 - 2024. All rights reserved.