Pythonic gui format?

G

Gregory Petrosyan

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?)
I am currently seeking for pythonic alternative for XML. One possible
way is to create a class for every (type of?) window, like

class SomeDlg(Window):
def __init__(self, dict?):
self.setdict(dict)
self.add_text("$friend would like to speak to you. $question")
self.add_button(action=self.onclick,
text="Yeah, I'd like to $action !")
...

def onclick(self):
# process data here, send some messages etc.


Isn't it ugly a bit? Another variant is

dlg = Window(dict, [opts])
dlg.text("text here", [opts])
dlg.image(src, pos, text [etc])
.....

or possibly

dlg = Window(...)
dlg.text = Text(...)
dlg.button = Button(...)
....
dlg.display(surface)


These styles of course can be combined... But IMHO looks not very nice
:( Maybe I shall not rely on introspection? Any suggestions, comments,
thoughts and links are welcome.
Thanks.
 
I

Ivan Voras

Gregory said:
I have to write _simple_ gui library, for embedding into game. My
first attempt was to use XML
....

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?)

Don't do that :)

Don't listen to recommendations for or against a technology based only
on vague "this is not how we do things around here" reasons. If XML
suits your need, than use it. It seems it will work fine in situations
where you need to edit/extend the GUI by external means. If you do it in
code, you risk that you'll need to change the code every time you want
to reposition a GUI element. It doesn't matter much if you invent your
own file format because eventually you'll have to extend it, or write
support for it, so you could as well use XML from the start.

XML parsing can be a little tedious, but there are many tools that make
it easier (I've even written one: http://ivoras.sharanet.org/xmldict.py.gz).
 
G

Gregory Petrosyan

Thanks for your reply.
But isn't python code more flexible than XML? I think it's not harder
to edit py file than xml file. Also python can give me more speed than
xml, and possibly python code can be integrated better with rest of app
(because of absence of extra layer called "XML"). Has anybody have had
(how many grammar errorrs can you find here? :) some experience in
writing gui systems/formats "from scratch"?

IMO the strength of XML here is that it provides one, almost obvious,
format for gui description. And with Python, I can imagine hundreds of
them :) (but maybe there are real gems, and after finding one I'll be
very happy?)
 
G

Georg Brandl

Gregory said:
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>

One could imagine to model this with Python, like this:

class FooDlg(Window):
title = "Hello World!"
size = (400, 300)

class WelcomeImg(Image):
filename = "..."
pos = (0, 0)

class Box(Frame):
text = "My Controls"

class NameLbl(Label):
text = "Name:"

class NameField(TextBox):
length = 100
pos = ...
def onClick(self):
MessageBox(self.text)

class TestList(ListBox):
items = ['First', 'Second']

_Slight_ misuse of "class" though...

Georg
 
G

Gregory Petrosyan

Nice, thanks!
BTW, maybe decorators could do good job here (something like @expose in
TG?)
 
J

James Stroud

Ivan said:
Don't do that :)

Don't listen to recommendations for or against a technology based only
on vague "this is not how we do things around here" reasons.

The reasons given in the blog were fairly precise. I think "only on
vague[...]" is misleading here. The idea of the article was to educate a
Java user how to change his or her frame of reference to better use Python.

The author was not being territorial as you are implying.
 
P

Paul Boddie

Gregory said:
Thanks for your reply.
But isn't python code more flexible than XML? I think it's not harder
to edit py file than xml file.

It's all about integration with tools, and while there are various
situations where avoiding tools is often better than extensive use of
tools (Python vs. Java-plus-Eclipse being an example that springs to my
mind, at least), there are some very good tools for certain kinds of
GUI design; it's better in such situations to make use of those tools
rather than eschew them in favour of something "Pythonic" but, in
various respects, less usable.

Once upon a time, it may have been the case that GUI design tools
(typically for forms-based applications) only produced program code - I
think SpecTcl, for example, was admired for its capabilities but
criticised for its Tcl-centric nature, ultimately forcing many to look
elsewhere despite variants such as SpecPython - but most design tools
now seem to produce some kind of XML description of widgets and
dialogues. How usable to other languages would Glade or Qt Designer be
if they respectively produced just C and C++ code? (Actually, unlike
Tcl, consuming C or C++ produced from the same tools would be an
annoyance in itself, I'd imagine.)

Paul
 
G

Gregory Petrosyan

I need format, specialised for manual editing.
(And in this case a) I don't want/need to develop/find GUI forms
editors etc; b) maybe python is better for manual work? XML is pretty
nice for machines, but what about human mind: what is closer to it?)

Anyway, thanks for your help & attention.
 
B

Bruno Desthuilliers

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 ?)
 
C

Christoph Zwerschke

Bruno said:
Gregory Petrosyan a écrit :

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

window = {
'title' : 'Hello World!'
'image' : {'text' :"nice picture here",
...

I think this is pretty much the approach of PythonCard
(http://pythoncard.sf.net) with its resource files.

Bruno, before writing another simple GUI, have a look at PythonCard.
Maybe it already does what you want.

-- Christoph
 
G

Gregory Petrosyan

Isn't it ugly a bit?
I'd even say 'ugly 16-bits' !-)

You are right of course. Those "examples" are really bad, and, most
of all, really un-pythonic.

Thanks for JSON. It's more clean&simple than XML, but my main idea is
to remove any extra layer between Python and GUI. I want all GUI
elements/data to be directly accessible from Python (without extra
libraries).
Your dicts example is nice, but this approach (and some others) lacks
one important feature: ordering of GUI elements. In XML, the order of
all elements is specified, and with dicts (or with very clean Georg's
model) it is not. (BTW remember topics about ordered dicts...)

I think that there should be a way for solving this problem, and I'll
certainly try to find it.
Thanks for your help.
 
D

DH

bruno said:
No, it's pure Python. It happens that JSON looks pretty close to Python,
but that's another point.

Python dict and lists ARE JSON. The only difference that python can't
handle is multiline comments.
 
D

DH

Gregory said:
Thanks for JSON. It's more clean&simple than XML, but my main idea is
to remove any extra layer between Python and GUI. I want all GUI
elements/data to be directly accessible from Python (without extra
libraries).

Since JSON is just python dicts and lists, you don't need an extra
library to use it, essentially.

Your dicts example is nice, but this approach (and some others) lacks
one important feature: ordering of GUI elements. In XML, the order of
all elements is specified, and with dicts (or with very clean Georg's
model) it is not. (BTW remember topics about ordered dicts...)

That's a good point.
 
B

bruno at modulix

DH said:
Python dict and lists ARE JSON. The only difference that python can't
handle is multiline comments.

And what about true vs True and false vs False ?-)

No, Python's dicts and lists are not JSON. They are Python's dicts and
lists. JSON stands for JavaScript Object Notation, and AFAIK, Python and
javascript are two different languages (even if they are pretty close on
some points).
 
B

bruno at modulix

DH said:
Since JSON is just python dicts and lists, you don't need an extra
library to use it, essentially.

Please stop with this nonsense. Python is Python, JSON is Javascript
Object Notation, and even if the mapping is quite straightforward, you
just can't substitute one for the other. BTW, there *are* some
Python<->JSON Python packages...
 
B

bruno at modulix

Gregory said:
You are right of course. Those "examples" are really bad, and, most
of all, really un-pythonic.

Thanks for JSON. It's more clean&simple than XML, but my main idea is
to remove any extra layer between Python and GUI. I want all GUI
elements/data to be directly accessible from Python (without extra
libraries).
Your dicts example is nice, but this approach (and some others) lacks
one important feature: ordering of GUI elements. In XML, the order of
all elements is specified, and with dicts (or with very clean Georg's
model) it is not. (BTW remember topics about ordered dicts...)

One possible (but somewhat ugly) solution is to use a list of tuples
instead of a dict:

d = {'k1': 'v1', 'k2': 'v2'}
=>
l = [('k1', 'v1'), ('k2', 'v2')]

where 'values' can of course be any Python data type...


Another solution is to include ordering infos in the dict, either as
1/an entry for each item or as 2/a separate entry:

1/
window = {
"item" : {'k1': 'v1', 'k2': 'v2', 'order': XXX},
"otheritem" : {'k1n': 'v1n', 'k2n': 'v2n', 'order': YYY},
}

2/
window = {
"item" : {'k1': 'v1', 'k2': 'v2'},
"otheritem" : {'k1n': 'v1n', 'k2n': 'v2n'},
"order" : ['item', 'otheritem'],
}

NB : I'd rather choose the second solution...


And finally, you could write your own ordered mapping type - but then
you loose the builtin syntax...

My 2 (unordered) cents...
 
C

Christoph Zwerschke

bruno said:
<OT>
Sorry, Christoph, wrong attribution !-)
</OT>

I'm sorry, I still get easily confused by usenet multilevel quoting ;-)
I meant Gregory and I meant "GUI library", not "GUI".

-- Christoph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top