Read Programming Python Online

Authors: Mark Lutz

Tags: #COMPUTERS / Programming Languages / Python

Programming Python (47 page)

BOOK: Programming Python
9.17Mb size Format: txt, pdf, ePub
ads
Python GUI Development Options

Before we start wading
into the tkinter pond, let’s begin with some perspective on
Python GUI options in general. Because Python has proven to be such a good
match for GUI work, this domain has seen much activity over the years. In
fact, although tkinter is by most accounts still the most widely used GUI
toolkit in the Python world, there are a variety of ways to program user
interfaces in Python today. Some are specific to Windows or X
Windows,
[
24
]
some are cross-platform solutions, and all have followings
and strong points of their own. To be fair to all the alternatives, here
is a brief inventory of GUI toolkits available to Python programmers as I
write these words:

tkinter

An open
source GUI library and the continuing de facto
standard for portable GUI development in Python. Python scripts that
use tkinter to build GUIs run portably on Windows, X Windows (Unix
and Linux), and Macintosh OS X, and they display a native
look-and-feel on each of these platforms today. tkinter makes it
easy to build simple and portable GUIs quickly. Moreover, it can be
easily augmented with Python code, as well as with larger extension
packages such as
Pmw
(a third-party widget
library);
Tix
(
another widget library, and now a standard part of
Python);
PIL
(
an image-processing extension); and
ttk
(
Tk themed widgets, also now a standard part of Python
as of version 3.1). More on such extensions like these later in this
introduction.

The underlying Tk library used by tkinter is a standard in the
open source world at large and is also used by the Perl, Ruby, PHP,
Common Lisp, and Tcl scripting languages, giving it a user base that
likely numbers in the millions. The Python binding to Tk is enhanced
by Python’s simple object model—Tk widgets become customizable and
embeddable objects, instead of string commands. tkinter takes the
form of a module package in Python 3.X, with nested modules that
group some of its tools by functionality (it was formerly known as
module
Tkinter
in Python 2.X, but was renamed
to follow naming conventions, and restructured to provide a more
hierarchical organization).

tkinter is mature, robust, widely used, and well documented.
It includes roughly 25 basic widget types, plus various dialogs and
other tools. Moreover, there is a dedicated book on the subject,
plus a large library of published tkinter and Tk documentation.
Perhaps most importantly, because it is based on a library
developed
for scripting languages,
tkinter is also a relatively lightweight toolkit, and as such it
meshes well with a scripting language like Python.

Because of such attributes, Python’s tkinter module ships with
Python as a standard library module and is the basis of Python’s
standard IDLE integrated development environment GUI. In fact,
tkinter is the only GUI toolkit that is part of Python; all others
on this list are third-party extensions. The underlying Tk library
is also shipped with Python on some platforms (including Windows,
Mac OS X, and most Linux and Unix-like systems). You can be
reasonably sure that tkinter will be present when your script runs,
and you can guarantee this if needed by freezing your GUI into a
self-contained binary executable with tools like
PyInstaller and py2exe (see the Web for
details).

Although tkinter is easy to use, its text and canvas widgets
are powerful enough to implement web pages, three-dimensional
visualization, and animation. In addition, a variety of systems aim
to provide GUI builders for Python/tkinter today, including GUI
Builder (formerly part of the Komodo IDE and relative of SpecTCL),
Rapyd-Tk, xRope, and others (though this set has historically tended
to change much over time; see
http://wiki.python.org/moin/GuiProgramming
or search
the Web for updates). As we will see, though, tkinter is usually so
easy to code that GUI builders are not widely used. This is
especially true once we leave the realm of the static layouts that
builders typically support.

wxPython

A Python
interface for the open source wxWidgets (formerly
called wxWindows) library, which is a portable GUI class framework
originally written to be used from the C++ programming language. The
wxPython system is an extension module that wraps wxWidgets classes.
This library is generally considered to excel at building
sophisticated interfaces and is probably the second most popular
Python GUI toolkit today, behind tkinter. GUIs coded in Python with
wxPython are portable to Windows, Unix-like platforms, and Mac OS
X.

Because wxPython is based on a C++ class library, most
observers consider it to be more complex than tkinter: it provides
hundreds of classes, generally requires an object-oriented coding
style, and has a design that some find reminiscent of the MFC class
library on Windows. wxPython often expects programmers to write more
code, partly because it is a more functional and thus complex
system, and partly because it inherits this mindset from its
underlying C++ library.

Moreover, some of wxPython’s documentation is oriented toward
C++, though this story has been improved recently with the
publication of a book dedicated to wxPython. By contrast, tkinter is
covered by one book dedicated to it, large sections of other Python
books, and an even larger library of existing literature on the
underlying Tk toolkit. Since the world of Python books has been
remarkably dynamic over the years, though, you should investigate
the accuracy of these observations at the time that you read these
words; some books fade, while new Python books appear on a regular
basis.

On the other hand, in exchange for its added complexity,
wxPython provides a powerful toolkit. wxPython comes with a richer
set of widgets out of the box than tkinter, including trees and HTML
viewers—things that may require extensions such as Pmw, Tix, or ttk
in tkinter. In addition, some prefer the appearance of the
interfaces it renders. BoaConstructor and wxDesigner, among other
options, provide a GUI builder that generates wxPython code. Some
wxWidgets tools also support non-GUI Python work as well. For a
quick look at wxPython widgets and code, run the demo that comes
with the system (see
http://wxpython.org/
, or
search the Web for links).

PyQt

A Python
interface to the Qt toolkit (now from Nokia, formerly
by Trolltech), and perhaps the third most widely used GUI toolkit
for Python today. PyQt is a full-featured GUI library and runs
portably today on Windows, Mac OS X, and Unix and Linux. Like
wxPython, Qt is generally more complex, yet more feature rich, than
tkinter; it contains hundreds of classes and thousands of functions
and methods. Qt grew up on Linux but became portable to other
systems over time; reflecting this heritage, the PyQt and PyKDE
extension packages provide access to KDE development libraries
(PyKDE requires PyQt). The BlackAdder and Qt
Designer
systems provide GUI builders
for PyQt.

Perhaps Qt’s most widely cited drawback in the past has been
that it was not completely open source for full commercial use.
Today, Qt provides both GPL and LGPL open source licensing, as well
as commercial license options. The LGPL and GPL versions are open
source, but conform to GPL licensing constraints (GPL may also
impose requirements beyond those of the Python BSD-style license;
you must, for example, make your source code freely available to end
users).

PyGTK

A Python
interface to GTK, a portable GUI library originally
used as the core of the Gnome window system on Linux. The
gnome-python and PyGTK extension packages export Gnome and GTK
toolkit calls. At this writing, PyGTK runs portably on Windows and
POSIX systems such as Linux and Mac OS X (according to its
documentation, it currently requires that an X server for Mac OS X
has been installed, though a native Mac version is in the
works).

Jython

Jython (the
system formerly known as JPython) is a Python
implementation for Java, which compiles Python source code to Java
bytecode, and gives Python scripts seamless access to Java class
libraries on the local machine. Because of that, Java GUI libraries
such as
swing
and
awt
become another way to construct GUIs
in Python code run by the JPython system. Such solutions are
obviously Java specific and limited in portability to that of Java
and its libraries. Furthermore,
swing
may be one of the largest and most
complex GUI option for Python work. A new package named
jTkinter
also provides a tkinter port to
Jython using Java’s JNI; if
installed
, Python scripts may also use
tkinter to build GUIs under Jython. Jython also has Internet roles
we’ll meet briefly in
Chapter 12
.

IronPython

In a very similar vein, the IronPython system
—an implementation of the Python language for the .NET
environment and runtime engine, which, among other things, compiles
Python programs to .NET bytecode—also offers Python scripts GUI
construction options in the .NET framework. You write Python code,
but use C#/.NET components to construct interfaces, and applications
at large. Iron
Python
code can
be run on .NET on Windows, but also on Linux under the Mono
implementation of .NET, and in the Silverlight client-side RIA
framework for web browsers (discussed ahead).

PythonCard

An open source GUI
builder and library built on top of the wxPython
toolkit and considered by some to be one of Python’s closest
equivalents to the kind of GUI builders familiar to Visual Basic
developers. PythonCard describes itself as a GUI construction kit
for building cross-platform desktop applications on Windows, Mac OS
X, and Linux, using the Python language.

Dabo

An open
source GUI builder also built on wxPython, and a bit
more. Dabo is a portable, three-tier, cross-platform desktop
application development framework, inspired by Visual FoxPro and
written in Python. Its tiers support database access, business
logic, and user interface. Its open design is intended to eventually
support a variety of databases and multiple user interfaces
(wxPython, tkinter, and even HTML over HTTP).

Rich Internet Applications (RIAs)

Although web pages
rendered with HTML are also a kind of user interface,
they have historically been too limited to include in the general
GUI category. However, some observers would extend this category
today to include systems which allow browser-based interfaces to be
much more dynamic than traditional web pages have allowed. Because
such systems provide widget toolkits rendered by web browsers, they
can offer some of the same portability benefits as web pages in
general.

The going buzzword for this brave new breed of toolkits is
rich Internet applications
(RIAs). It includes
AJAX and JavaScript-oriented frameworks for use on the client, such
as:

Flex

An open source framework from Adobe and part of the
Flash platform

Silverlight

A Microsoft framework which is also usable on
Linux with Mono’s Moonlight, and can be accessed by Python
code with the IronPython system described above

JavaFX

A
Java platform for building RIAs which can run
across a variety of connected devices

pyjamas

An AJAX-based port of the Google Web Toolkit to Python,
which comes with a set of interface widgets and compiles
Python code that uses those widgets into JavaScript, to be run
in a browser on a client

The
HTML5
standard under development
proposes to address this domain as well.

Web browsers ultimately are “desktop” GUI applications, too,
but are more pervasive than GUI libraries, and can be generalized
with RIA tools to render other GUIs. While it’s possible to build a
widget-based GUI with such frameworks, they can also add overheads
associated with networking in general and often imply a
substantially heavier software stack than traditional GUI toolkits.
Indeed, in order to morph browsers into general GUI platforms, RIAs
may imply extra software layers and dependencies, and even multiple
programming languages. Because of that, and because not everyone
codes for the Web today (despite what you may have heard), we won’t
include them in our look at traditional standalone/desktop GUIs in
this part of the book.

See the Internet part for more on RIAs and user interfaces
based on browsers, and be sure to watch for news and trends on this
front over time. The interactivity these tools provide is also a key
part of what some refer to as “Web 2.0” when viewed more from the
perspective of the Web than GUIs. Since we’re concerned with the
latter here (and since user interaction is user interaction
regardless of what jargon we use for it), we’ll postpone further
enumeration of this topic until the next part of the book.

Platform-specific options

Besides the portable toolkits like tkinter, wxPython, and
PyQt, and platform-
agnostic
approaches such as RIAs, most major platforms have nonportable
options for Python-coded GUIs as well. For instance, on
Macintosh OS X, PyObjC
provides a Python binding to Apple’s Objective-C/Cocoa
framework, which is the basis for much Mac development. On
Windows, the
PyWin32 extensions package for Python includes
wrappers for the C++ Microsoft Foundation Classes (MFC)
framework (a library that includes interface
components), as well as Pythonwin, an MFC sample program that
implements a Python development GUI. Although .NET technically runs
on Linux, too, the IronPython system mentioned earlier offers
additional Windows-focused options.

BOOK: Programming Python
9.17Mb size Format: txt, pdf, ePub
ads

Other books

Roadside Sisters by Wendy Harmer
Midnight Man by Lisa Marie Rice
The Only Road by Alexandra Diaz
Fatal North by Bruce Henderson
Unexpected Magic by Diana Wynne Jones
The Spanish Connection by Nick Carter
Jake's Wake by Cody Goodfellow, John Skipp


readsbookonline.com Copyright 2016 - 2024