【问题标题】:wxpython: How do you access the objects which you've placed inside of a wx.Sizer object?wxpython:如何访问放置在 wx.Sizer 对象中的对象?
【发布时间】:2013-01-16 19:49:21
【问题描述】:

在重构会话期间,我将大量的 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 内部的东西吗?我想我可以稍微扩展一下函数,然后返回对象列表以及大小调整器,或者简单地转储函数并将标识符保留给我需要在 main 中修改的对象……但是……我的 gui 代码已经够意大利面了。如果我能避免它,我愿意。

【问题讨论】:

    标签: python wxpython


    【解决方案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> >
    

    【讨论】:

    • 不幸的是没有运气。我尝试了IsWindowGetWindow。两者都返回了 AttributeErrors。
    • 啊,你说得对。感谢您的编辑。我最初在主尺寸器上调用Window,没有意识到我需要在实际尺寸器上这样做。谢谢!
    【解决方案2】:

    其实我几个月前就回答过这种问题: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上写了一篇关于这个主题的文章。

    【讨论】:

      【解决方案3】:

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

      还有另一种方法可以在子创建过程中使用name 参数,即:

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

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

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

      此方法在此示例中可能无法直接使用,因为您需要确保唯一的子名称才能访问它们。你会怎么做取决于你。

      【讨论】:

      • wx.FindWindowByName 已弃用。请改用 wx.Window.FindWindowByName。另外,当名称不唯一时会发生什么?
      • FindWindowByName 方法将在找到第一个匹配项后立即停止,您将无法访问任何可能的后续匹配项。至少我不知道这种可能性,但我不是 wxpyhton 专家......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多