All Window
objects in an application are connected in various ways. Quite often it is useful to get a reference to an instance of a control so that you can perform some operation on the control or retrieve some data from it. This recipe will show some of the facilities that are available for finding and getting references to controls.
Here we extend the MyFrame
class from the previous recipe to have an event handler for when its Button is clicked. In the event handler we can see some ways to access different controls in our UI during runtime:
class MyFrame(wx.Frame): def __init__(self, parent, id=wx.ID_ANY, title="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, name="MyFrame"): super(MyFrame, self).__init__(parent, id, title, pos, size, style, name) # Attributes self.panel = wx.Panel(self) self.panel.SetBackgroundColour(wx.BLACK) button = wx.Button(self.panel, label="Get Children", pos=(50, 50)) self.btnId = button.GetId() # Event Handlers self.Bind(wx.EVT_BUTTON, self.OnButton, button) def OnButton(self, event): """Called when the Button is clicked""" print "\nFrame GetChildren:" for child in self.GetChildren(): print "%s" % repr(child) print "\nPanel FindWindowById:" button = self.panel.FindWindowById(self.btnId) print "%s" % repr(button) # Change the Button's label button.SetLabel("Changed Label") print "\nButton GetParent:" panel = button.GetParent() print "%s" % repr(panel) print "\nGet the Application Object:" app = wx.GetApp() print "%s" % repr(app) print "\nGet the Frame from the App:" frame = app.GetTopWindow() print "%s" % repr(frame)
Each window in the framework keeps a reference to its parent and to its children. Running our program now will print out the results of using the accessor functions that all windows have for finding and retrieving references to their children and other related controls.
GetChildren
: This method will return a list of all of the children that the given control hasFindWindowById
: This can be used to find a specific child window by using its IDGetParent
: This method will retrieve the window's parent windowwx.
GetApp
: This is a global function for getting access to the one and only application objectApp.
GetTopWindow
: This gets the main Top-Level Window in the application
Clicking on the Button
will cause the OnButton
method to be called. In OnButton
, there are examples that show how to use each of the above methods. Each of them will return a reference to a GUI object. In our example, calling GetChildren
on the Panel
will return a list of its children controls. Iterating over this list, we print out each of the children, which will just be the Button in this case. FindWindowById
can be used to find a specific child control; again, we called this on our Panel
to find the Button
control. Just to show that we found the Button
, we used its SetLabel
method to change its label. Next, calling GetParent
on the Button
will return the Button
's parent, which is the Panel
. Finally, by using the global GetApp
function, we can get a reference to the application object. The App
object's GetTopWindow
will return a reference to our Frame.
Here are a few more useful methods available for getting references to controls.