Pythonic gui format?

Patrik Blommaskog patrik at olga.removethispart.mine.removethispart.nu
Tue Feb 14 14:55:46 EST 2006


How about this:


def mogrify():
   print "Mogrifying"

gui = window(title = "Hello World!") [
         image(text = "nice pics here", pos = (5, 5), src = "img.png"),
         text(opts = "italic") ["(some text here)"],
         lst() [
           "first element",
           "second element"
         ],
         button(action = mogrify, text = "Click Me!")
       ]


There may be some slight errors above, but the principle works for me. 
The gui is described with valid Python code, and object order, types, 
parameter checking etc works in the way you would expect in Python. If 
you try not to think about how it abuses Pythons items, I find it quite 
nifty.

I have a toy project where I build a gui like this, with two major 
differences from your wishlist: (1) My code does not contain the actual 
widget objects, but instead builds a tree that gets traversed to build 
the gui objects. (2) The only parser I have right now builds html 
documents with callback dispatching (using Turbogears). I envision 
generation of gui code for locally executed GUI:s (e g wxPython) as 
well. However, I see no immediate reason that you should not be able to 
put the actual widget objects in the tree like illustrated above.

I did not in any way invent this method of description. If you are 
interested, a more mature use can be found in Nevow 
(http://divmod.org/trac/wiki/DivmodNevow) where they have a document 
object model called Stan, using a similar method to generate xhtml 
documents. I'd be happy to hear about other uses.

I'll leave it to others to have opinions about whether it is to be 
considered pythonic.


- Patrik



More information about the Python-list mailing list