Creating Linux Applications Using PyGTK
September 30th, 2008I’ve looked at using python to create GUIs in Linux a few times but coming from a background of using Visual Basic it seemed very different and the learning curve a bit steep. This time I decided to really give it a new try and I have to say that it’s way easier than I expected.
My first recommendation is to use glade-3. Last time I used glade-2 but I have a real problem with the multiple windows concept as windows keep disappearing behind other application windows. If you use gtk.Builder then the glade file created by glade-3 needs to be converted to a compatible xml file before it can be loaded, but this is easy to achieve with the following command.
$ gtk-builder-convert gui.glade gui.xml
From there I was amazed how easy it was to attach events and quickly write code, a very basic example to load an xml file an attach events is shown here.
This is the xml file created by using glade and running gtk-builder-convert. It’s just a window containing a button.
<?xml version="1.0"?>
<!--Generated with glade3 3.4.4 on Tue Sep 30 11:46:18 2008 -->
<interface>
<object class="GtkWindow" id="window1">
<signal handler="on_window1_destroy" name="destroy"/>
<child>
<object class="GtkButton" id="button1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="label" translatable="yes">gtk-close</property>
<property name="use_stock">True</property>
<signal handler="on_button1_clicked" name="clicked"/>
</object>
</child>
</object>
</interface>
This is the python code to run the gui, very simple indeed.
#!/usr/bin/env python
import pygtk
import gtk
pygtk.require("2.0")
class GUI(object):
def __init__(self):
builder = gtk.Builder()
builder.add_from_file("GUI.xml")
builder.connect_signals(self)
self.window1 = builder.get_object("window1")
self.window1.show()
def on_window1_destroy(self,widget,data=None):
gtk.main_quit()
def on_button1_clicked(self,widget,data=None):
gtk.main_quit()
if __name__ == "__main__":
app = GUI()
gtk.main()
To run just save the python file as gui.py, chmod +x the python file and then run using ./gui.py. Just make sure the xml file is in the same directory as the python file.
I’ll post more pygtk as I learn but so far I’m amazed how easy it is to create very professional looking applications in a very short time. There are thousands of tutorials on pygtk on the net but I found this one very useful.
http://www.micahcarrick.com/12-24-2007/gtk-glade-tutorial-part-1.html
October 24th, 2009 at 7:45 pm
GtkBuilder: Merging XML Definitions…
The documentation for GtkBuilder currently states:
A GtkBuilder is an auxiliary object that reads textual descriptions of a user interface and instantiates the described objects. To pass a description to a GtkBuilder, call gtk_builder_add_from_file() …
March 6th, 2010 at 3:41 pm
Thanks for the post. Keep the simple tutorial coming!
May 24th, 2010 at 8:59 am
You should use the window’s delete event instead of the destroy event I guess. At least at my end it doesn’t work this way (The window disappears but I don’t get the CLI prompt back).
October 1st, 2011 at 3:18 am
[...] http://blog.bobpeers.com/2008/09/30/creating-linux-applications-using-pygtk/ LD_AddCustomAttr("AdOpt", "1"); LD_AddCustomAttr("Origin", "other"); [...]