Pythonic gui format?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Feb 13 20:58:17 EST 2006


Gregory Petrosyan a écrit :
> Buenos dias, amigos!
> I have to write  _simple_  gui library, for embedding into game. My
> first attempt was to use XML: isn't it cute to describe ui in such a
> way:
> 
> <window>
>     <title>Hello World!</title>
>     <image text="nice picture here" pos=... src=... />
>     <text opts=...>
>         (some text here)
>     </text>
>     <list>
>         <item>first element</item>
>         <item>second one...</item>
>     </list>
>     <button action=... text="Click Me!" />
> </window>
> 
> (or something similar)
> 
> But after reading "Python Is Not Java"
> http://dirtsimple.org/2004/12/python-is-not-java.html  (BTW I'm not a
> Java programmer) I realised that it is actually not cute at all :) (am
> I wrong here?)

Well, XML is a bit on the verbose side (but less than Java, which is why 
Javaers love XML), and we usually have better ways to express things in 
Python. But this doesn't mean that you shouldn't use it when appropriate.

> I am currently seeking for pythonic alternative for XML. 

A pretty obvious one is dicts and lists. What about (Q&D):

window = {
  'title' : 'Hello World!'
  'image' : {'text' :"nice picture here",
              'pos' : ...,
              'src' : .. },
  'text_opts' : """
          (some text here)
   """,
   'items' : [
	'first-element',
	'second-one',
         ...
         ]
    'button' : {
	'action': ...
         'text' :"Click Me!"
    },
}


well... It sure needs more work, but you get the idea...

> One possible
> way is to create a class for every (type of?) window, like
> 
(snip ugly code)
> 
> Isn't it ugly a bit? 

I'd even say 'ugly 16-bits' !-)

> These styles of course can be combined... But IMHO looks not very nice
> :( Maybe I shall not rely on introspection?

Why not ?

> Any suggestions, comments,
> thoughts and links are welcome.

You may want to have a look at JSON. It's 'JavaScript Object Notation' - 
but Python and javascript are close enough, so the translation process 
is a no-brainer - and it's used as a replacement for XML in most 
Pythonic AJAX implementations. One of the potential plus of JSON (vs 
more python-specific solutions) is that it's language-independant.

Else, there was a quite interesting configuration module by Vinay Sajip 
that could fit your needs:

http://www.red-dove.com/python_config.html
http://www.red-dove.com/config/

(BTW, is this module still maintained ?)



More information about the Python-list mailing list