Hooking things up in GUI application

sturlamolden sturlamolden at yahoo.no
Tue Apr 25 07:52:37 EDT 2006


Ryan Ginstrom wrote:

> Apropos recent threads about GUI editors, coming from a Win32/WTL C++
> background, I actually like the idea of being able to (easily) create GUIs
> programmatically.
>
> But I still see a lot of the same tedium: hooking up events to handlers, and
> getting data into and out of dialogs. In C++, this is generally handled
> through code generation and/or macros, but IMO these are brittle and ugly.
>
> So my question: Is there a Pythonic way to make these tedious hookups easier?


If you use PyGTK (it also runs on Windows), you can design the GUI with
GLADE and then use libglade to import the gui as an xml-resource. You
have to write all handlers (you do anyway), but the GUI design becomes
entirely visual and the hookup becomes one single like of code. It
doesn't get easier than that. As a bonus you get an hardware
accelerated GUI (Cairo takes care of that, currently through OpenGL but
a DirectX backend is planned). I.e. a hardware accelerated GUI
represented as an XML resource. Isn't that Microsoft's next 'big thing'
codenamed 'Avalon'? I wonder where they got the idea.

This should illustrate the level of tediousness when using PyGTK:

import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade

class MyGUI:

   def __init__(self):
      win1 = libglade.GladeXML('mygui.glade','window1')
      win1.signal_autoconnect(self)   # hook event handlers, pass any
class or dictionary
      win1.maximize()

   def on_win1_destroy(self):
      gtk.main_quit()

if __name__ == '__main__'
   gui = MyGUI()
   gtk.main()


You cannot make it less tedious in C++. Even if you use Visual Studio
and all sorts of 'wizards' to autogenerate code. Actually, if you do
use C, C++ or Ada, GLADE can autogenerate code just like Visual Studio.
But since an XML resource us much easier to modify and maintain, it is
not recommended.




More information about the Python-list mailing list