GladeApp
Home Home Contact us Contact
home News Projects Tests Files Follow manatlan on Twitter Sign in
Last edition : 2008/10/14 21:11

GladeApp

GladeApp is a class to help you to developp Pygtk Application with Glade interface.

It takes the idea behind "tepache/SimpleGladeApp" from Tigrus Writing PyGTK applications in a visual way. (AFAIK, it's not maintain anymore)

Tepache used the "patch/diff mechanism" to generate/update a pygtk class structure from a glade file.(You'd just to modify your GUI with glade, save the glade file, and pass it thru the tepache.py to update your py file, containing a python class wrapping around your widget)

GladeApp is similar, except you don't have the need to use tepache to update your py file. You just inherit your class from GladeApp. And at runtime, it will warn you from the missing signals. You'll have just to copy/paste them in your code, and implement them. (it's like GladeGen)

The simplest example (using window.glade which contains a button, and 2 events):

A python:

import os
from gladeapp import GladeApp

class Window(GladeApp):
    glade=os.path.join(os.path.dirname(__file__), 'window.glade')

    def init(self):
        pass

    def on_button1_clicked(self,*args):
        print "hello"

    def on_window1_delete_event(self, *args):
        self.quit()

w=Window()
w.loop()

* TO BE CONTINUED TO BE CONTINUED TO BE CONTINUED *

GladeApp is available on the SVN account of Jbrout.

Download GladeApp




In progress / Future

NOT RELEASED YET

I'm working on the ability to describe widgets tree in the docstring of the class.

An example:

class MyWindow(GladeApp):
    """
    Window win
        .title="Hello"
        @delete_event
        VBox
            Label jo
                .label="Who are you ?"
            HBox
                Label
                    .label = Name
                entry eName
                    .text=""
            HBox
                LaBel
                    .label="Email"
                gtkEntry eMail
                    .text=""
            Button b
                .label="Ok"
                @clicked
    """
    def init(self,m):
        self.jo.set_text(m)

    def on_b_clicked(self,*a):
        self.quit(3)

    def on_win_delete_event(self,*args):
        self.quit(4)

f=MyWindow("Qui est tu ?")
print f.loop()

How it works :

In fact, the docstring is converted as a "glade file". The tree of widgets is indented, like python code.

  • line starting with a dot is an property of the parent widget.
  • line starting with a @ is a declaration of a event of the parent widget.
  • otherwise it's a child widget ...

Widgets are not case sensitive (and can start with "Gtk" or not). If a second word is provided it will be used as an "id", otherwise an "unique id" will be build according the type of widget.

The window is created with glade, widgets are provided as attributs of the instance, and signals are autoconnected on method "on _ {widgetId} _ {event} (self,*args)" (if a signal is missing in your class, it warns you at runtime)

It will not replace the use of glade-3 for complex widget ... but I think it's an easy way to code very quickly a simple pygtk window/form.

I think it could be really useful/handly... If you know well the names of gtk widget/event/properties, you can code your gtk-GUI as your code your python, in your favorite editor, without the need to use glade-3 to change/add/modify a glade file. Code and Interface stays together ... It's a real breeze ...

AND BETTER : GladeObj !!

GladeObj give the ability to build its own gtk objects ! It's not dependant from GladeApp, and can be used in a common pygtk app. An example:

class MyHbox(GladeObj):
    """
    HBox
        Entry ee
        Button bm       # button for decrement "ee"
            .label=-
            @clicked
        Button bp       # button to increment "ee"
            .label=+
            @clicked
    """
    def init(self,v):
        self.ee.set_text(str(v))
    def on_bm_clicked(self,*args):
        self.ee.set_text(str( int(self.ee.get_text())-1 ))
    def on_bp_clicked(self,*args):
        self.ee.set_text(str( int(self.ee.get_text())+1 ))

a = MyHBox(12)

"a" is now a REAL Gtk.HBox, and can be packed/added to any container !

RSS Python Powered Get Ubuntu
©opyleft 2008-2019 - manatlan