up with PyGUI!

Carlos Ribeiro carribeiro at gmail.com
Sun Sep 26 16:49:39 CEST 2004


On Sun, 26 Sep 2004 08:28:10 -0400, Ed Leafe <ed at leafe.com> wrote:
>         Forgive me if this is an oversimplification, but it looks like what
> you want is an XML-based definition of your UI objects, and are trying
> to approximate that with Python syntax. What you've written looks an
> awful lot like an XRC file structure, albeit without the closing tags.

Here we go -- Sunday morning, coding and discussing...

I'm avoiding a data-driven approach (XML) because I think that keeping
all declarations in Python will make my code easy to work with in the
long run. I've tried some XML based tools, but found the process to be
too convoluted -- it needs another set of tools and skills, and it
means that I need to be able to read yet another completely different
syntax. Also, I'm trying to avoid visual GUI designers at this point
-- I realized that I simply lose to much time futzing with pixel
alignment details, and that I'm better investing my time doing other
stuff. I've read a comment in the Wing IDE website that puts it very
well:

http://wingware.com/doc/howtos/wxpython
"""
A Caveat: Because Python lends itself so well to writing data-driven
code, you may want to reconsider using a GUI builder for some tasks.
In many cases, Python's introspection features make it possible to
write generic GUI code that you can use to build user interfaces on
the fly based on models of your data and your application. This can be
much more efficient than using a GUI builder to craft individual menus
and dialogs by hand. In general hand-coded GUIs also tend to be more
maintainable.
"""

(It's interesting to note that although Wing folks talk about
"data-drive code", it really applies very well to the declarative
approach)


But while interesting, all this discussion is just revolving around a
few points. I think that we agree on more things than we disagree. We
mostly disagree with the (ab)use of Python as a tool to declare
complex nested elements. I don't think this is a real problem, and I
think that Python can add enough power to the declarations as to make
them more flexible than it's possible using XML or other similar data
format. It's easy to write small blocks of reusable declarations in
Python, and it's easy to mix code and static data on the declarations.
This is more difficult with a XML approach. For example:

class Username(EditBox):
    label = _('User name')
    size  = 12

class UserForm(Form):
    class username(Username):
        datalink = userfield
    class password(EditBox):
        label = _('Password')
        size  = 12
        datalink = password
        password = True

In the example above, I'm using two features:

1) It's possible to call functions in the middle of the declaration.
In the example, the _() call it's a shorthand convention for gettext()
internationalization code. I like the way it's presented in the class
declaration. It's visible and easily editable.

2) The Username class is declared outside of the form. In this way it
can be used for several form declarations. It's harder to do the same
for individual elements of design with XML. Also, the use of
inheritance makes it clear that some of the parameters can be
individually overriden on each form.

As a side note, the fact that many classes are nested makes easier to
manage the namespace. It's something that we also get for free with
this design.

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list