Book Image

wxPython 2.8 Application Development Cookbook

By : Cody Precord
Book Image

wxPython 2.8 Application Development Cookbook

By: Cody Precord

Overview of this book

<p>In today’s world of desktop applications there is a great amount of incentive to be able to develop applications that can run in more than one environment. Currently there are a handful of options available for cross platform frameworks to develop desktop applications in Python. wxPython is one such cross- platform GUI toolkit for the Python programming language. It allows Python programmers to create programs with a complete, highly functional graphical user interface, simply and easily. wxPython code style has changed quite a bit over the years, and gotten much more Pythonic. The examples you will find in this book are right up to date and reflect this change in style.<br />This cookbook provides you with the latest recipes to quickly create robust, reliable, and reusable wxPython applications. These recipes will guide you from writing simple, basic wxPython scripts all the way through complex concepts, and also feature various design approaches and techniques in wxPython.<br /><br />The book starts off by covering a variety of topics from the most basic requirements of a wxPython application to some of the more in depth details of the inner workings of the framework laying the foundation for any wxPython application. It then explains event handling, basic and advanced user interface controls, designing and layout, creating dialogs, components and extending functionality, and so on. We conclude by learning how to build and manage applications for distribution.<br />For each of the recipes, there is an introductory example, then more advanced examples, and plenty of example code to develop and manage user-friendly applications. For more experienced developers, most recipes also include additional discussion of the solution, allowing you to further customize and enhance the component.</p>
Table of Contents (19 chapters)
wxPython 2.8 Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Referencing controls


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.

How to do it...

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)

How it works...

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 has

  • FindWindowById: This can be used to find a specific child window by using its ID

  • GetParent: This method will retrieve the window's parent window

  • wx. GetApp: This is a global function for getting access to the one and only application object

  • App. 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.

There's more...

Here are a few more useful methods available for getting references to controls.

Function Name

Description

wx.FindWindowByLabel(label)

Finds a child window by looking for it by Label

wx.FindWindowByName(name)

Finds a child window by looking for it by Name

wx.GetTopLevelParent()

Gets the Top-Level Window, which is at the top of the given control's parental hierarchy

See also

  • The Understanding the window hierarchy recipe in this chapter outlines the structure of how windows are contained within and are related to each other.