Book Image

wxPython Application Development Cookbook

By : Cody Precord
Book Image

wxPython Application Development Cookbook

By: Cody Precord

Overview of this book

wxPython is a GUI toolkit for the Python programming language built on top of the cross-platform wxWidgets GUI libraries. wxPython provides a powerful set of tools that allow you to quickly and efficiently building applications that can run on a variety of different platforms. Since wxWidgets provides a wrapper around each platform’s native GUI toolkit, the applications built with wxPython will have a native look and feel wherever they are deployed. This book will provide you with the skills to build highly functional and native looking user interfaces for Python applications on multiple operating system environments. By working through the recipes, you will gain insights into and exposure to creating applications using wxPython. With a wide range of topics covered in the book, there are recipes to get the most basic of beginners started in GUI programming as well as tips to help experienced users get more out of their applications. The recipes will take you from the most basic application constructs all the way through to the deployment of complete applications.
Table of Contents (17 chapters)
wxPython Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Displaying hierarchical data with TreeCtrl


TreeCtrl provides a way to create and display data in a hierarchy. The control contains a series of nodes that can have child nodes, which in turn can have their own child nodes. This nested data display allows users to expand and collapse the nodes to see more or less as need be. In this recipe, we will use TreeCtrl to create an outline view of an XML file.

How to do it…

Perform the following steps:

  1. For this recipe, in addition to the wx module, we will use the ElementTree module from the Python standard library. Take a look at the following script:

    import xml.etree.ElementTree as ET
    import wx
  2. Next, let's start on a custom subclass of wx.TreeCtrl to specialize it to display XML data using the following code:

    class XMLOutliner(wx.TreeCtrl):
        def __init__(self, parent, xmlText):
            super(XMLOutliner, self).__init__(parent)
            
            rootElement = ET.fromstring(xmlText)
            root = rootElement.tag
            self._root = self.AddRoot(root...