On Thu, Jan 16, 2014 at 4:02 AM, Sergio Tortosa Benedito
<serto...@gmail.com> wrote:
> Hi I'm developing a sort of language extension for writing GUI programs
> called guilang, right now it's written in Lua but I'm considreing Python
> instead (because it's more tailored to alone applications). My question
> it's if I can achieve this declarative-thing in python. Here's an
> example:
>
> Window "myWindow" {
>         title="Hello world";
>         Button "myButton" {
>                 label="I'm a button";
>                 onClick=exit
>         }
> }
> print(myWindow.myButton.label)

Probably the easiest way to do that would be with dictionaries or
function named arguments. It'd be something like this:

myWindow = Window(
    title="Hello World",
    myButton=Button(
        label="I'm a button",
        onClick=exit
    )
)
print(myWindow.myButton.label)

For this to work, you'd need a Window class that recognizes a number
of keyword arguments (eg for title and other attributes), and then
takes all other keyword arguments and turns them into its children.
Possible, but potentially messy; if you happen to name your button
"icon", it might be misinterpreted as an attempt to set the window's
icon, and cause a very strange and incomprehensible error.

The syntax you describe there doesn't allow any flexibility in
placement. I don't know how you'd organize that; but what you could do
is something like GTK uses: a Window contains exactly one child, which
will usually be a layout manager. Unfortunately the Python GTK
bindings don't allow such convenient syntax as you use here, so you'd
need some sort of wrapper. But it wouldn't be hard to set up something
like this:


def clickme(obj):
    print("You clicked me!")
    obj.set_label("Click me again!")

myWindow = Window(
    title="Hello World",
    signal_delete=exit
).add(Vbox(spacing=10)
    .add(Button(label="I'm a button!", signal_clicked=exit))
    .add(Button(label="Click me", signal_clicked=clickme))
)

This is broadly similar to how I create a window using GTK with Pike,
and it's easy to structure the code the same way the window is
structured.

If the number of helper classes you'd have to make gets daunting, you
could possibly do this:

myWindow = GTK(Gtk.Window,
    title="Hello World",
    signal_delete=exit
).add(GTK(Gtk.Vbox, spacing=10)
    .add(GTK(Gtk.Button, label="I'm a button!", signal_clicked=exit))
    .add(GTK(Gtk.Button, label="Click me", signal_clicked=clickme))
)

so there's a single "GTK" class, which takes as its first positional
argument a GTK object type to clone. This is all perfectly valid
Python syntax, but I don't know of a convenient way to do this with
the current modules, so it may require writing a small amount of
wrapper code.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to