PyGTK and Glade:
HelloUbuntu.glade:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.2.0 on Sun Aug 5 16:51:48 2007 by noneofyourbusiness@NowhereYouKnow-->
<glade-interface>
<widget class="GtkWindow" id="wndMain">
<property name="visible">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<property name="title" translatable="yes">Welcome to Ubuntu</property>
<signal name="destroy" handler="on_wndMain_destroy"/>
<child>
<widget class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<child>
<widget class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<child>
<widget class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<property name="xpad">13</property>
<property name="ypad">5</property>
<property name="label" translatable="yes">What is your name?
</property>
</widget>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="padding">17</property>
</packing>
</child>
<child>
<widget class="GtkEntry" id="txtName">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
</widget>
<packing>
<property name="position">1</property>
</packing>
</child>
</widget>
</child>
<child>
<widget class="GtkButton" id="btnWelcome">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<property name="label" translatable="yes">Welcome me!</property>
<signal name="clicked" handler="on_btnWelcome_clicked"/>
</widget>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="lblWelcome">
<property name="visible">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
</widget>
<packing>
<property name="position">2</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>
HelloUbuntu.py
Code:
#!/usr/bin/env python
import gtk
import gnome.ui
import gtk.glade
#Load the widget tree.
class HelloUbuntu:
def __init__(self):
self.wTree = gtk.glade.XML("HelloUbuntu.glade")
dic = {"on_wndMain_destroy":self.destroy_signal,"on_btnWelcome_clicked":self.click_signal}
self.wTree.signal_autoconnect(dic)
def destroy_signal(self,widget):
gtk.main_quit()
def click_signal(self,widget):
name = self.wTree.get_widget("txtName").get_text()
self.wTree.get_widget("lblWelcome").set_text("Hello " + name + ", welcome to Ubuntu!")
if __name__== "__main__":
app = HelloUbuntu()
gtk.main()
Put the .glade file in the same directory as the .py file, and then run the python file.
I've been meaning to get off my butt and learn to use Glade to build PyGTK interfaces. This made me do it, and it took just about twenty minutes to learn!
Bookmarks