GUI Development v3

19 downloads 12174 Views 3MB Size Report
Apr 8, 2015 - There are other application development tools in Python that will let you ... application are a graphical user interface (GUI) and event handlers.
GUI Development v3 April 8, 2015

1

Rapid Application Development with TraitsUI

In this tutorial, we will build on our prior lessons of image processing and classes to start to develop graphical applications in Python. Python has several frameworks for developing graphical applications, most noteably: - PyQt - WxPython Developing directly in QT or WX gives you a tremendous amount of flexibility and control over the graphical elements of the program. However, the development cycle with these frameworks can be fairly long. In GUI development, there’s a tradeoff between framework flexibility and development cycle time. There are other application development tools in Python that will let you design your applications faster, but sacrifice some flexibilty. In particular, Enthought has developed a very nice solution to rapid graphical application design through their collection of libraries known as the Enthought Tool Suite. These include: -

Traits TraitsUI Chaco Mayavi Enaml And others.

We will focus on Traits and TraitsUI, although, TraitsUI has been unofficially supplanted by Enaml. While we won’t touch on Enaml in this tutorial, users should consider using it in place of TraitsUI. For a really nice overview of using Traits and TraitsUI, Check out this guide by Gael Varoquax. 1.0.1

Dependencies

Before getting started, you will need the following library packages (available through Canopy Package Manager if not pre-installed already) - traits (current version 4.5) - traitsui (current version 4.4) - scikits-image And their corresponding dependencies 1.0.2

What is the distinction between an application and a pure python program? When do I want to use them?

Strangely enough, I made a screencast about application programing in python a couple years ago. It will explain the difference between so-called “imperative programming” vs. “graphical programming”. Check it out:

1

In [1]: from IPython.lib.display import YouTubeVideo YouTubeVideo(’ohHoU4qvsNs’) Out[1]: The key aspects of an application are a graphical user interface (GUI) and event handlers. Event handlers are like pipelines: when an event is triggered (e.g. the radius of a circle class is changed), other variables must be handled (e.g. the area should also be updated). In building an interactive application, the complexity of events grows quickly. Furthermore, the event handlers must sync up to the graphical user interface. Therefore, the major hurdles with building graphical applications come down to these issues: • Building complex event handlers is messy. • Graphical User Interface must trigger events properly. 1.0.3

Traits and TraitsUI to the rescue

To build applications with complex event handlers, we’ll use the traits and traitsui libraries. traits is a Python library that implements very straightforward event handling. traitsui will build a graphical user interface from the traits code. Let’s revisit the Circle class from your homework. Instead of a standard python object, let’s make this a traits object by inheriting from the HasTraits parent class. For now, forget about area and only focus on the radius attribute. In [2]: from traits.api import HasTraits, Float class Circle(HasTraits): # We no longer use __init__ to define our variables. # you want to be interactive, is defined like this: radius = Float(5.0)

Any variable that

#