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