Step-by-step instructions for a "Hello World" using Python, gtk, pygtk (which Debian calls python-gtk), and libglade. o Run glade. It'll pop up three windows. o Create a new project: File/New Project o Edit the options: File/Project Options... o Set the Project Name and Project Directory, hit Ok. I'm setting my project name to pyglade. o In the Palette window, under the GTK+ Basic tab, the icon in the upper-left looks like a window. Click it to create a new window. o In the Properties window, change the Title from "window1" to whatever you want the X11 window title to be. Now, the very most important thing to remember as you use glade is that GTK uses a container model. Every grayed-out hole that you see can hold exactly one widget. So, all you can do with the window that you have created is select a widget from the Palette window and drop it into your application's window. If you didn't drop a container (Horizontal Box, Vertical Box, Table, or Fixed Positions) that can hold multiple widgets, you can't add any more widgets. For our example, we're just displaying "Hello World" (boring yes, but a necessary first step), so we'll insert a label. o Click on the capital A in the Palette window. o Click in the application's window. o The entire application's window now says "label1". o Go to the Properties window, and change the Label to it says "Hello World" instead of "label1". The application window will change as you type. In case you're wondering, glade has no concept of "accept what I've done". Everything you do happens as you do it, and there's no going back. Save early, save often. o In the Glade: window, hit the Save button or File/Save. You have now created the glade file. Mine is called pyglade.glade. Now to create the Python program which activates the file. o Create the following Python program: ---- #!/usr/bin/env python import gtk, libglade # We put all of our gtk signal handlers into a class. This lets us bind # all of them at once, because their names are in the class dict. class GladeHandlers: pass class WidgetsWrapper: def __init__(self): self.widgets = libglade.GladeXML ('pyglade.glade', "window1") self.widgets.signal_autoconnect(GladeHandlers.__dict__) # Gives us the ability to do: widgets['widget_name'].action() def __getitem__(self, key): return self.widgets.get_widget(key) widgets = WidgetsWrapper() gtk.mainloop () ---- o Run the program. It will create a tiny window saying "Hello World". Unfortunately, the program swallows sigint, so we can't kill it with a ^C. You can kill it using ^Z and kill %jobnumber. Let's modify the program so it has a quit button. o Go back to the Glade: window. o Edit the options: File/Project Options... o Change the project name to pyglade1. o Right-click on the Label widget to bring up the menu. o Select "Cut" from the menu. The label will disappear. o From the Palette window, select the Vertical Box. It's icon looks like three boxes piled on top of each other. o Click on the application's window. This will pop up a dialog asking you how many rows you want. Set it to 2. o You now have two greyed-out boxes, and vbox1 is in the Properties window. o Right-click in the upper box and select Paste. o label1 now occupies the top entry in vbox1. o In the Palette, select the button saying "Ok". o Click in the remaining gray box in the application window. o Change the label of the button in the Properties window to "Quit" instead of "button1". o Change the name of the button in the Properties window to "quit" instead of "button1". o In the Properties window, click the Signals tab. o To the right of the Signal: line is a button with three dots. Click it to bring up the Select Signal dialog. o Select the "clicked" signal from this dialog, and hit Ok. o This will also fill in the Handler: box with "on_quit_clicked". This is correct, so hit "Add". o Save. You just created pyglade1.glade. Here's pyglade1.py, which handles the quit button: ---- #!/usr/bin/env python import gtk, libglade # We put all of our gtk signal handlers into a class. This lets us bind # all of them at once, because their names are in the class dict. class GladeHandlers: def on_quit_clicked(event): gtk.mainquit() class WidgetsWrapper: def __init__(self): self.widgets = libglade.GladeXML ('pyglade1.glade', "window1") self.widgets.signal_autoconnect(GladeHandlers.__dict__) # Gives us the ability to do: widgets['widget_name'].action() def __getitem__(self, key): return self.widgets.get_widget(key) widgets = WidgetsWrapper() gtk.mainloop () ---- o The only changes are the name of the GladeXML file, and the addition of the on_quit_clicked handler. o Let's add another button that changes the text of label1. o Change the project name to pyglade2 o Decide where you want the button to be. Right-click on either "Hello World" or Quit. Pick the vbox1 sub-menu. Select insert-before or insert-after as you wish. You'll get a new grey box. o If you inserted it in the wrong place, right-click and select "Delete". o It'll be called button2. You can change either the name or label if you want, using the Properties window's Widget tab, but I won't. o In the Properties Signals tab, add the "clicked" signal. It'll be button2 again. Add it. o Save. Here's the python code: ---- #!/usr/bin/env python import gtk, libglade # We put all of our gtk signal handlers into a class. This lets us bind # all of them at once, because their names are in the class dict. class GladeHandlers: def on_quit_clicked(event): gtk.mainquit() def on_button2_clicked(event): widgets['label1'].set_text("spam, spam, spam") class WidgetsWrapper: def __init__(self): self.widgets = libglade.GladeXML ('pyglade2.glade', "window1") self.widgets.signal_autoconnect(GladeHandlers.__dict__) # Gives us the ability to do: widgets['widget_name'].action() def __getitem__(self, key): return self.widgets.get_widget(key) widgets = WidgetsWrapper() gtk.mainloop () ---- o The only changes are the GladeXML file and the addition of the on_button2_clicked signal handler. Feel free to play around with glade and these test programs. libglade creates the widgets for you, so you can insert widgets without writing any python code to handle their signals. For example, if you want to put vbox1 into a frame widget, you can do so without changing the Python code. Or you can change the order of widgets in vbox1. Or create more vboxes and hboxes, and put the widgets wherever you want. They're identified by name and not position or hierarchy. One thing to watch out for is that the Gnome icons aren't supported in pygtk, so if you set a button to be a Stock Button, or if you insert a toolbar with buttons, pygtk will not be able to render them properly. Various useful homepages: o pygtk: http://quoll.daa.com.au/~james/pygtk/ o gtk: http://www.gtk.org o Python: http://www.python.org The pygtk mailing list archives are searchable and often have answers straight from James Henstridge.