Rapid Application Development with Python and Glade
GladeGen first determines whether the specified module/file exists; if not, it creates a basic file with author information and a class definition. GladeGen then parses the Glade XML file and finds a list of widgets and handlers. Python provides the inspect module that allows a program to determine what functions, classes and methods an existing Python module contains and which lines correspond to each. GladeGen uses the inspect module to determine which callbacks have been written already so that they are not replaced with an empty callback method. GladeGen adds any new callbacks to the bottom of the class definition. The inspect module also allows GladeGen to determine which lines contain the init method and to replace them with a new init method containing all the widgets and handlers in the latest Glade XML file.
Python supports both the standard DOM and SAX interfaces for parsing XML files. The SAX interface is an event-driven model in which the user sets up functions to be called as XML tags are processed. The DOM interface reads the entire XML file into memory and provides functions for traversing the XML hierarchy and retrieving the information. For GladeGen, we wanted to extract only certain information from the XML file, so the DOM interface is simpler to use. Also, the size of a Glade XML file is small enough that reading the entire file into memory and generating the Python representation of it should not require a large amount of memory. Using the DOM interface, the get_xml method in the GladeGen class extracts the widget names and handler names from a Glade XML file using about 30 lines of Python code.
Glade and GladeGen automate much of the tedious, repetitive work that goes into creating graphical programs by removing the need to write code to create and store widgets and set up the callback functions. This allows for rapid application development of Python/GNOME/GTK applications. The finished Math Flash is shown in Figure 2. The GladeGen software can run on any system that supports Python and GTK, including Linux, UNIX, Mac OS X and Microsoft Windows.
A number of features could be added to this system. Instead of using the generic *args parameter for the created callback functions, the parameters could be specified explicitly, based on the widget and callback prototype. I also plan to add a graphical front end to the program for configuring the options in the GladeGenConfig.py file. The GladeGen software is released under the GPL. If anyone is interested in modifying/extending it, please let the author know.
Thanks to one of my students, Jeremiah Schilens, who worked on an earlier version of this project with me.
Resources for this article: /article/7558.
David Reed lives in Columbus, Ohio with his wife and two dogs. He has worked with UNIX systems since 1991 and Linux since 1997. He holds a PhD in volumetric graphics from The Ohio State University and currently teaches computer science at Capital University. Capital uses a mixture of Python, C++ and Java throughout its CS curriculum. David can be reached at dreed@capital.edu.
- « first
- ‹ previous
- 1
- 2
- 3
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Dynamic DNS—an Object Lesson in Problem Solving
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- Tech Tip: Really Simple HTTP Server with Python
- Roll your own dynamic dns
37 min 36 sec ago - Please correct the URL for Salt Stack's web site
3 hours 49 min ago - Android is Linux -- why no better inter-operation
6 hours 4 min ago - Connecting Android device to desktop Linux via USB
6 hours 32 min ago - Find new cell phone and tablet pc
7 hours 31 min ago - Epistle
8 hours 59 min ago - Automatically updating Guest Additions
10 hours 8 min ago - I like your topic on android
10 hours 54 min ago - This is the easiest tutorial
17 hours 30 min ago - Ahh, the Koolaid.
23 hours 9 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?





Comments
I have a better solution...
In www.oneminutepython.com I show you how to develop an application in only one minute! It contains direct links to key (all open source) applications (python, wxpython etc.) and a template application.
You saved my day!
I am pulling my hair off for almost 4 hours as to why my gtk window is not appearing... it was because I did not write the window.show() statement. No blog/article I have found until now include that line. I even reinstalled my glade and python thinking the error was with them.
Thank a looooooot!!!
Shrikant Sharat
Re: Rapid Application Development with Python and Glade
What about signal names which includes '-'
Here's my solution
-------------------
"""GWidget - Making use of glade files easier.
:See:
GWidget
"""
import sys
import gtk
from gtk import glade
import gobject
class GWidget(object):
"""Wrapper for widgets in glade file.
Each toplevel widget in a glade file can be associated with a GWidget
class. Methods of that class if has prefix `on_`, are treated as
signal handlers for widgets under the tree of toplevel widget in
glade file. Signal handlers are of the form::
on_widgetname__signalname
``signalname`` if contains hyphens should be replaced with
underscores. If signal handler method name additionally ends with
double underscore (__), then ``connect_after`` is used to connect
that method.
``on_quit`` method if defined will be connected to ``delete-event``
event of the toplevel widget.
Any widget in the glade file (comming under toplevel widget) can be
accessed as if there is an instance variable named by that widget name.
Toplevel widget is always accessible through the ``widget`` instance
variable.
:CVariables:
- `GLADE_FILE`: Path of the glade file to use.
- `TOPLEVEL_NAME`: Name of the toplevel widget.
"""
# Path of glade file
GLADE_FILE = None
# Toplevel widget name
TOPLEVEL_NAME = None
# Properties
gladexml = property(lambda self: self._gladexml, doc='GladeXML object')
widget = property(lambda self: self._widget, doc='Toplevel widget')
def __init__(self, autoconnect_now=True):
"""
:Parameters:
- `autoconnect_now`: If False, doesn't autoconnect signals
"""
self._gladexml = glade.XML(self.GLADE_FILE, self.TOPLEVEL_NAME)
self._widget = self.gladexml.get_widget(self.TOPLEVEL_NAME)
if hasattr(self, 'on_quit'):
self._widget.connect('delete-event', self.on_quit)
self._widgets_cache = {} # Widgets are stored in cache for
# quicker future retrieval
if autoconnect_now:
self.autoconnect_signals()
def autoconnect_signals(self, prefix='on_'):
"""Autoconnect methods with unique format with widget actions.
See the class documentation string for more info.
"""
conn_func = gobject.GObject.connect
conn_after_func = gobject.GObject.connect_after
wid_in = len(prefix)
for key in self.__class__.__dict__:
hdl = getattr(self, key)
if callable(hdl) and key.startswith(prefix):
# Assumption: signal names doesn't contain double-underscores
if key.endswith('__'):
key = key[:-2]
connect_func = conn_after_func
else:
connect_func = conn_func
sig_in = key.rfind('__')
if sig_in >sys.stderr, 'Unknown callback method %s' % key
continue
wid_name = key[wid_in:sig_in]
sig_name = key[sig_in+2:]
sig_name.replace('_', '-')
new_widget = self._gladexml.get_widget(wid_name) or getattr(self, wid_name)
if new_widget is not None:
connect_func(new_widget, sig_name, hdl)
else:
print >>sys.stderr, 'Handler error for [%s]. No such widget [%s]' % (key, wid_name)
def __getattr__(self, attr):
try:
# Return the Gtk widget if cached
return self._widgets_cache[attr]
except KeyError:
# Return widget from gladexml
new_widget = self._gladexml.get_widget(attr)
if new_widget is not None:
self._widgets_cache[attr] = new_widget
return new_widget
# AttributeError
raise
GladeGenConfig.py file is not python
The GladeGenConfig.py file is a gzip file according
to the "file" command.
When renamed with a gz suffix and unzipped the
resulting file is a tar file which contains...
-rwxrwxr-- jill/ljedit 10867 2004-04-21 10:27:26 GladeGen.py
-rwxrwxr-- jill/ljedit 4992 2004-04-21 10:27:26 GladeWindow.py
-rw-rw-r-- jill/ljedit 2438 2004-04-21 10:27:39 MathFlash-create.py
-rw-rw-r-- jill/ljedit 2170 2004-04-21 10:27:39 MathFlash-create-short-lines.py
-rw-rw-r-- jill/ljedit 25161 2004-04-21 10:27:52 mathflash.glade
-rw-rw-r-- jill/ljedit 279 2004-04-21 10:27:52 mathflash.gladep
-rwxrwxr-x jill/ljedit 4752 2004-04-21 10:27:39 MathFlash.py
-rwxrwxr-x jill/ljedit 4752 2004-04-21 10:27:39 MathFlash.py-latest
But still no GladeGenConfig.py file....
GladeGenConfig.py problem
Appears to be a binary file in the .tgz
Re: GladeGenConfig.py problem
It appears to have been fixed.
The author should have posted that here.