wxPython: How to get sizer from wx.StaticText?

ⅰ亾dé卋堺 提交于 2019-12-11 01:49:19

问题


The widget is in one of many sizers I make, but how to get sizer of one of these widgets, for example in wx.StaticText. First, I though wx.StaticText have a method GetSizer() because it derived from wx.Window, but it always return None, is there a way?

Sorry for my poor language.

EDIT (08/23/2012) Solution from Mike Driscoll:

Using self.sizer.GetChildren() to get SizerItemList from some sizer, then using GetWindow() to get actual widget from the list


回答1:


If the sizer has children, then GetChildren does return a list of widgets. I've done it many times with wxPython 2.8. I don't remember anyone mentioning it was different in 2.9 or Phoenix, so I'm guessing it's not. Can you tell us which OS and wxPython version you're using?

If you want to know how to get an arbitrary sizer, you might try GetContainingSizer or use the Widget Inspection Tool

EDIT (08/22/2012): Here's a working example:

import wx

########################################################################
class MyApp(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Example")
        panel = wx.Panel(self)

        lbl = wx.StaticText(panel, label="I'm a label!")
        txt = wx.TextCtrl(panel, value="blah blah")
        btn = wx.Button(panel, label="Clear")
        btn.Bind(wx.EVT_BUTTON, self.onClear)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(lbl, 0, wx.ALL, 5)
        self.sizer.Add(txt, 0, wx.ALL, 5)
        self.sizer.Add(btn, 0, wx.ALL, 5)

        panel.SetSizer(self.sizer)

    #----------------------------------------------------------------------
    def onClear(self, event):
        """"""
        children = self.sizer.GetChildren()

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

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyApp()
    frame.Show()
    app.MainLoop()


来源:https://stackoverflow.com/questions/12031522/wxpython-how-to-get-sizer-from-wx-statictext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!