LWN.net Logo

Using Parasite to delve into GTK+ applications

March 3, 2009

This article was contributed by Ben Martin

Trying to debug your own GUI applications can be a pain if you are not extremely familiar with the toolkit used to make the user interface. When the code you are working on is also unfamiliar, the whole experience quickly becomes less than desirable. The GTK+ Parasite tool helps you to work out the structure of the widgets that comprise a GUI, inspect and change properties of those widgets, and perform more in-depth analysis using an embedded Python shell.

The GTK+ toolkit provides an object-oriented framework for making user interfaces in C. GTK+ gives you facilities to inspect and change the properties of objects and supports introspection so you don't need to know about classes at compile time in order to make use of them at runtime. The GTK+ Parasite attaches itself to a GTK+ application and takes advantage of these dynamic features to let you inspect and change the interface of an application as it runs.

GTK+ Parasite doesn't have any official releases yet, but the source can easily be pulled from its Git repository, compiled and installed using the standard ./autogen.sh && make autotools dance. You'll want to make sure that you have the development packages for PyGtk installed first in order to get the embedded Python shell functionality.

To use GTK+ Parasite, add its name to the GTK_MODULES environment variable and run your GTK+ application as you normally would. For example:

    GTK_MODULES=gtkparasite gedit

Along with your application, you should see and additional Parasite window with a Widget Tree and Action List tab and a small area in the lower part of the window with a Python prompt.

[GTK+ Parasite screenshot] To find out the hierarchy of widgets in your GTK+ application, click on the Inspect button in the Parasite window and then any part of the GUI of your GTK+ application. Along with each widget in a tree view you should see if that widget is realized, mapped and visible, along with the address of both the X Window of the widget and the GTK+ widget itself. The latter address is very handy because you can right click on it and "Send Widget to Shell" to obtain a reference to the widget from the embedded Python interpreter.

The list in the far right of the Widget Tree tab in the Parasite window lists the properties and their value for the current widget. Holding the left button down over a property pops up a list of possible values for you to change it too. On the other hand, if the range of values for a property is too large, like for an integer property, no menu is presented and you can enter the value directly.

The Action List tab in Parasite shows you all the GTKAction objects in the application. For those unfamiliar with the GTK+ toolkit, a GtkAction object represents a piece of functionality that can be connected to a menu or toolbar, for example, opening a Save as dialog or starting a search within the current document. As an example, running Parasite on the text editor gedit, finding the FileOpen action in the list and selecting "Send Object to Shell" from the menu, you can perform the GtkAction by calling the activate method on the object. You should see the file dialog appear. The embedded Python shell command should look something like:

    >>> parasite.gobj(0xa78980).activate()

where everything up to the .activate() was added automatically by Parasite when I told it to send the object to the shell.

If you are writing a custom GTK widget, the "Show Graphic Updates" button causes any redraws that the application performs to briefly flash red first. This makes it fairly simple to see if you are drawing more than you think in order for your widget to update itself. For example, in gedit, only a rectangle covering the current line is updated when you type text into the active document, but when you hit return the current line and everything below it flashes red.

There are a few rough edges to the GUI of GTK+ Parasite, which is to be expected from such a young application. For example, in the "Action List" tab, one might expect to be able to simply double-click on an action to execute it. In addition, left-clicking on a property lets you either edit the value directly inline in the cell or if there is a limited number of acceptable values a popup menu appears allowing you to select a value. While this provides a consistent user interface for editing though left clicking, it does mean that you have to click on a property row before editing its value. One might at first expect a context menu to be available offering such editing functionality, with the added bonus that you could directly right click on a property to edit it rather than having to select it with a left click first.

For such a young application GTK+ Parasite is already very useful and a great tool for ironing out the kinks in an application's GTK+ interface. If you are a Python fan, the embedded Python interpreter lets you tinker with the GTK+ interface even if the program itself is written in C.

Parasite is developed by Christian Hammond and David Trowbridge. Activity on the mailing list is currently on the slow side, but it should pick up as developers discover this tool.


(Log in to post comments)

Copyright © 2009, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds