By: Mark Mruss
GUI programming, like many other types of programming, can sometimes prove exhausting because you must repeat yourself over and over again. AVC is one tool available to Python GUI programmers that attempts to simplify things by synchronizing application data and GUI widgets.
Every once in a while I find myself browsing the Internet trying to find out what’s new and exciting in the Python world. Sometimes I browse to find topics for this article; other times mere curiosity draws me across the web. While I was browsing the other day, I stumbled across AVC: the Application View Controller . I was immediately intrigued by it because itsÃ¢Â€Â™ name is so similar to the Model View Controller (MVC) pattern. Being familiar with the Model View Controller pattern, and admittedly having struggles with it in the past, I decided to check out AVC to determine if it might be a viable alternative.
After reading about AVC I was intrigued for several reasons. The main reason was the promise of “a multiplatform, fully automatic, live connection among graphical interface widgets and application variables.”  This means that graphical widgets can be connected to variables and automatically synchronized. One of the (many?) problems with Graphical User Interface (GUI) programming is that you often find yourself doing the same thing over and over again. One of the things that you end of doing over and over again is setting the contents of a widget based on the value of a variable, and then subsequently, setting that variable’s value based on the current state of the widget. Whenever someone promises me an automatic connection between GUI widgets and my variables, I’m interested.
The other reason for investigating AVC is my past struggles with the Model View Controller pattern. I like the decoupling that the MVC pattern seems to provide, but I often feel as though I am needlessly duplicating code in order to stick with the pattern. I looked into AVC hoping to find a simple framework that will avoid this and guide the separation of my application data from my GUI logic. However in the end, I realized that AVC (in its current state), does not appear to achieve this across all widget toolkits. It does allow you to separate your application from much of the GUI logic that one would normally need, but it does not appear to allow you to fully separate the data from the GUI.
AVC is being developed by Fabrizio Pollastri, and is released under version 3 of the GPL. I was unable to find much information about the projectÃ¢Â€Â™s future or design objectives. But judging by the release notes, it is still being actively developed. The project is only at version 0.5 so donÃ¢Â€Â™t be surprised if the API (Application Programming Interface) or some of the goals change. Although relatively new, AVC bears further investigation because it already simplifies your GUI programming and contains the potential to do much more. In this article, I will introduce some of the main concepts behind AVC, and provide a small, working example using AVC and PyGTK.
AVC is a “Python module written in pure python” . This means that AVC is easy to install and does not have any dependencies aside from Python 2.2 or greater. You can download the current version (0.5.0) from the website or, if you are a lucky Linux user like me, you can install via your handy package manger. If you download the source, you can install it after extraction, using the following command:
python setup.py install
Depending on which widget toolkit you plan on using, the following requirements must also be met:
- GTK+: PyGTK 2.8 – 2.10
- Qt: PyQt or PyQt4v3 – v4
- Tk: Tkinter 2.4
- wxWidgets: wxPython 2.6
You can easily install any of the above toolkits using your favourite package manager, a source archive, or a binary installer found on each toolkitÃ¢Â€Â™s main website. Once AVC is installed, you can test the installation by importing the
avc module from the interactive interpreter. If all goes well you will see something similar to the following:
$ python Python 2.4.4 (#2, Jan 3 2008, 13:36:28) [GCC 4.2.3 20071123 (prerelease) (Debian 4.2.2-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import avc >>>
In its current state, AVC does not support all widgets available across all toolkits. It does currently support the following widgets:
- Check Box
- Combo Box (Not supported in Tk)
- Radio Button
- Spin Button
- Status Bar (Only supported by GTK+ and wxWidgets)
- Text View
- Toggle Button.
The support of each of these widget types is then handled by specific widgets in each toolkit.
In order to automatically synchronize variables and GUI widgets, AVC uses a name-matching technique. AVC supports two methods of name matching. The first method requires that the variable and the widget have the same name. For example, if you have a variable named
my_data it will match with a widget also named
my_data. In other words, if a variable and a widget have the same name they will be connected and their values synchronized.
The second name-matching technique allows you to connect variables to one or more widgets. Widgets can only be connected to one variable, but variables can be connected to any number of widgets. Using the second name-matching technique, you have a match if the widget name contains a double-underscore (
__), and the portion before the double-underscore matches the variable name. For example, if you have variable named
my_numeric_value and you want to match it with a Label widget and an Entry widget, you do this by naming the widgets as follows:
Again, the text before the “__” must match the variable name, and the text after the “__” can be whatever you want to differentiate your widgets or ensure uniqueness (if the toolkit requires it).
A GTK+, PyGTK, and AVC Example
For this AVC example, I will use the GTK+ widget toolkit and PyGTK. PyGTK a “is a set of bindings to the GTK+ user interface toolkit for the Python language” . This means that it lets us write GUI applications in Python using the GTK+ widget toolkit. To avoid any flame wars: I did not choose GTK+ because it is the best toolkit but because it is the toolkit with which I am the most familiar. In order to run this example you will need to have PyGTK 2.8 or later installed. You can get the latest version of PyGTK from the PyGTK website. 
As I mentioned earlier, AVC works with many different widgets types. It also has many different features. I won’t be covering all of the features or widgets types in this column; if you are interesting in learning about some of these features or how to work with different widgets types, please see the AVC documentation. 
This example is relatively simple, but it should illustrate some of the basic features of AVC. The example application asks for a personÃ¢Â€Â™s name and then displays that name in pop-up dialog. It will have the following features:
gtk.TextViewwidget to display the person’s name and allow the name to be edited. The
gtk.TextViewwidget will be synchronised with a variable.
gtkButtonthat will pop-up a
gtk.MessageDialogto show the person’s name.
gtk.Buttonthat will reset the name to the name with which the application was initialized.
To begin we need to import all of the modules necessary:
import gtk import avc.avcgtk
Note: I am using a
gtk.TextView widget instead of a
gtk.Entry widget. In its current state, AVC does not propagate changes in
gtk.Entry widgets back to the connected variable. I’m not sure if this is a current limitation or part of the design.
The above code imports the PyGTK module necessary to work with GTK+. It also imports the
avcgtk module, which is used to link our “application variables” with our GTK+ GUI. Depending on what widget toolkit you use, you will have to use different modules. For example, if you are working with Qt4, you will import:
The next step is to create the “application” class. This will be the class that contains the data. In this example it will also create the GUI:
Notice that we derive our application from the
avcgtk module’s AVC class. This is the GTK+ specific AVC class with which we will work. We will also create an
__init__ method in the
gtkAVC class. This is where we will initialize the data that will be connected to the GUI and create the GUI via the
def __init__(self, name=""): #set the variable and save the initial name self.initial_name = name self.name = name #setup GUI self.init_gui()
What we have here is pretty simple. We have the optional parameter
name that we store as the current name and as the initial name. We then call the
init_gui method that will create the GUI. The contents of the
init_gui method can be seen in Listing 1. I won’t explain most of this code since the majority of it is PyGTK specific. However I will make note of several relevant segments below.
def init_gui(self): #Setup the window and the layout manager self.window = gtk.Window() self.window.connect("destroy", gtk.main_quit) self.h_layout = gtk.HBox() self.window.add(self.h_layout) #create the widgets self.text_label = gtk.Label("Say hello:") self.h_layout.add(self.text_label) self.frame = gtk.Frame() self.h_layout.add(self.frame) self.scrolled_window = gtk.ScrolledWindow() self.frame.add(self.scrolled_window) self.text_view = gtk.TextView() self.text_view.set_name("name__text_view") self.scrolled_window.add(self.text_view) self.hello_button = gtk.Button("Hello!") self.hello_button.connect("clicked", self.on_hello_clicked) self.h_layout.add(self.hello_button) self.reset_button = gtk.Button("Reset") self.reset_button.connect("clicked", self.on_reset_clicked) self.h_layout.add(self.reset_button) self.window.show_all()
The only AVC specific code segment in the
init_gui function is where we set the name of the
This is all the code that we need to connect the
gtk.TextView widget with the
name variable that we created at the beginning of the
__init__ function. (Now, how’s that for simple?) Notice that the
name section of the “name__text_view” string will match with the
self.name variable. It is also important to note that the variable and the widget are not yet connected. They will become connected when the
avc_init method is called.
The other non-AVC related code of note is the connection of both
gtk.Button widgetsÃ¢Â€Â™ clicked signals with signal handlers. We connect the “hello” button to the
on_hello_clicked method and the “reset” button to the
That’s it for the
init_gui function, now let’s look at the two signal handlers. The
on_hello_clicked method, shown in Listing 2, simply displays the name in a
gtk.MessageDialog. What should be interesting to GUI programmers about this function is that instead of reading the data from the
gtk.TextView widget, we simply reference
self.name for the dialog’s text. We can do this because
self.name and the
gtk.TextView widget are connected and their values are always the same.
def on_hello_clicked(self, widget): def close(dialog, response): #Close the dialog on ok dialog.destroy() message_dlg = gtk.MessageDialog(self.window , 0 , gtk.MESSAGE_INFO , gtk.BUTTONS_OK , ("Hello: %s" % self.name)) message_dlg.connect("response", close) message_dlg.run()
on_reset_clicked function is also very simple and straightforward. We simply reset the name to the initial name:
def on_reset_clicked(self, widget): #Reset the name to the initial name self.name = self.initial_name
Again, notice that we don’t have to worry about doing anything to the
gtk.TextView widget that displays the current name. By simply modifying the data, what is displayed in the GUI will automatically be updated by AVC.
The next and final step in our test application is to create our class instances and actually show the GUI:
if (__name__ == "__main__"): gtk_avc = gtkAVC("Mark Mruss") gtk_avc.avc_init() gtk.main()
Since I am working in a Python file, I perform a simple test to make sure that the file is being launched directly instead of being imported as a module. I then create an instance of the
gtkAVC class setting the name to be my name. The
avc_init method is then called (which is a member of the
avc.avcgtk.AVC class). The
avc_init method is where all of the magic happens. It is where AVC goes through an application’s variables and connects them to the GUI widgets based upon the name matching technique explained earlier.
avc_init must be called after all of the data in the “application” and the widgets have been created.
The final step in this example, is to run the
gtk.main function. This is the “main loop” of our PyGTK based application and is standard when working with PyGTK.
The entire code can be seen in Listing 3. When you run the code you will be greeted with something similar to Figure 1. It may not seem like much, but what we have here is a fully functional AVC “application”. This simple “application” illustrates how easy it is to create an automatic connection between your data and your GUI.
#! /usr/bin/env python import gtk import avc.avcgtk class gtkAVC(avc.avcgtk.AVC): def __init__(self, name=""): #set the variable and save the initial name self.initial_name = name self.name = name #setup GUI self.init_gui() def init_gui(self): #Setup the window and the layout manager self.window = gtk.Window() self.window.connect("destroy", gtk.main_quit) self.h_layout = gtk.HBox() self.window.add(self.h_layout) #create the widgets self.text_label = gtk.Label("Say hello:") self.h_layout.add(self.text_label) self.frame = gtk.Frame() self.h_layout.add(self.frame) self.scrolled_window = gtk.ScrolledWindow() self.frame.add(self.scrolled_window) self.text_view = gtk.TextView() self.text_view.set_name("name__text_view") self.scrolled_window.add(self.text_view) self.hello_button = gtk.Button("Hello!") self.hello_button.connect("clicked", self.on_hello_clicked) self.h_layout.add(self.hello_button) self.reset_button = gtk.Button("Reset") self.reset_button.connect("clicked", self.on_reset_clicked) self.h_layout.add(self.reset_button) self.window.show_all() def on_hello_clicked(self, widget): def close(dialog, response): #Close the dialog on ok dialog.destroy() message_dlg = gtk.MessageDialog(self.window , 0 , gtk.MESSAGE_INFO , gtk.BUTTONS_OK , ("Hello: %s" % self.name)) message_dlg.connect("response", close) message_dlg.run() def on_reset_clicked(self, widget): #Reset the name to the initial name self.name = self.initial_name if (__name__ == "__main__"): gtk_avc = gtkAVC("Mark Mruss") gtk_avc.avc_init() gtk.main()
Benefits and Limitations
For a project that is little more than one year old (the first release was in January 2007) AVC appears quite stable and ready to be used. This is not to say that AVC is perfect. I did encounter a few issues.
The most notable issue that I encountered is the tightly coupled structure that AVC seems to impose on your application. For a module that tries to simplify the relationship between application data and GUI’s, this feels like a bit of an issue because. For example, when testing using PyGTK I was able to easily separate the GUI and the “application” into separate classes. But when I tried to do the same thing using PyQt4, I encountered an error because the
avc_timer function in the
avc.avcqt4.AVC module expects that the “application” class is itself a
QObject (namely the actual
QApplication). I find this counterintuitive since being able to separate the data from the GUI-logic seems to be a goal of AVC and similar tools. If you are able to separate the “application” fully from the GUI, you can easily connect your “application” to different GUI’s, and (by changing the AVC base class) even different widget toolkits.
I would also be interested in the ability to connect more types of data to more types of widgets. For example, I am interested in synchronizing Python lists with the contents of List boxes and Combo boxes. This is in addition to the current ability to synchronize the selection in the two widgets with integers.
Of course AVC also has a lot going for it. For one, I really like that AVC works with different widget toolkits. I have not seen this very often. It is a real benefit, especially when it comes to people adopting AVC. Hopefully in the future AVC will allow for further separation of the data and the GUI, truly enabling us to use different GUI’s and different widget toolkits with our data.
What I like most about AVC is its simplicity. I have played around with similar solutions and AVC is one of the leaders in terms of ease of use. I was able to get create a simple AVC based application in almost no time at all. (The time that it did take was spent trying to remember PyGTK.) Much of this has to do with the streamlined nature of AVC. There is very little to configure regardless of what widget toolkit you use. While this simplicity limits what AVC can currently achieve compared to other solutions, it also makes the barrier to entry low and the usage trivial. User friendliness is certainly a good thing in a package that aims to make GUI application development easier.
While the data that we worked with in the earlier example wasn’t that complicated (a simple string), I hope that it illustrates how helpful AVC is if you are working with a large amount of data or data that could be updated by an external source. Instead of having to worry about what widgets or toolkits the data was being displayed in, AVC lets us work with the data and GUI transparently, leaving the synchronization to AVC.
If what AVC offers sounds interesting to you, download it from the AVC website and try it out. Although it it’s not there just yet with enough attention, usage, and contributions from the Python community, AVC should reach full-feature status in no time.
Note: If you haven’t already, please check out my Python version poll.