Saturday, April 15, 2006

Glade, PyGTK, Python: Perfect Combination

Step 1: Install following program
# sudo apt-get install glade-2 python-glade2

Step 2: Create interface
Run your Glade, Application > Programming > Glade then simply add a button in a new GTK+ Window. There should be like this:

Add handler (on_button1_clicked) and signal (clicked) on properties.


save it, and ready to write Python code.

Step 3: Writing Python code
#!/usr/bin/python

import gtk
import gnome.ui
import gtk.glade

APPNAME="Hello"
APPVERSION="1.0"

class WidgetWrapper:
def __init__(self):
gnome.init(APPNAME, APPVERSION)
self.widgets = gtk.glade.XML("project5.glade")
self.widgets.get_widget("window1")

signalDic = { "on_button1_clicked" : self.on_button1_clicked }
self.widgets.signal_autoconnect(signalDic)

def on_button1_clicked(self, widget):
print "I'm success person"

if __name__ == "__main__":
Widgets = WidgetWrapper()
gtk.mainloop()

Step 4: Run it
Save your Python code in same directory with Glade project files. Give some name, hello.py for example then chmod 755 hello.py, run it. There would be "I'm success person" each time the button clicked. Try it!

0 Comments:

Post a Comment

<< Home