A Beginner's Guide to Using pyGTK and Glade

pyGTK and Glade allow anyone to create functional GUIs quickly and easily.
Creating the Python Program

First, we need to deal with any potential version skew. I use the following code, although a few other entries mentioned in the FAQ do similar things:

#!/usr/bin/env python

import sys

try:
 import pygtk
  #tell pyGTK, if possible, that we want GTKv2
  pygtk.require("2.0")
except:
  #Some distributions come with GTK2, but not pyGTK
  pass

try:
  import gtk
  import gtk.glade
except:
  print "You need to install pyGTK or GTKv2 ",
  print "or set your PYTHONPATH correctly."
  print "try: export PYTHONPATH=",
  print "/usr/local/lib/python2.2/site-packages/"
  sys.exit(1)

#now we have both gtk and gtk.glade imported
#Also, we know we are running GTK v2

Now are going to create a GUI class called appGUI. Before we do that, though, we need to open button1's properties and add a signal. To do that, click the three dots, scroll to clicked, select it and then click Add. You should end up with something like Figure 7.

Figure 7. After Adding the Event (Signal) Handler

With this in place, the signal_autoconnect causes any click of the button to call one of our functions (button1_clicked). You can see the other potential signals to be handled in that list as well. Each widget may have different potential signals. For example, capturing a text-changed signal on a text-entry widget may be useful, but a button never changes because it's not editable.

Initializing the application and starting gtk.mainloop() gets the ball rolling. Different event handlers need to have different numbers of arguments. The clicked event handler gets only one argument, the widget that was clicked. While you're at it, add the destroy event to the main window, so the program exits when you close the window. Don't forget to save your Glade project.

class appgui:
  def __init__(self):
    """
    In this init we are going to display the main
    serverinfo window
    """
    gladefile="project1.glade"
    windowname="serverinfo"
    self.wTree=gtk.glade.XML (gladefile,windowname)
    # we only have two callbacks to register, but
    # you could register any number, or use a
    # special class that automatically
    # registers all callbacks. If you wanted to pass
    # an argument, you would use a tuple like this:
    # dic = { "on button1_clicked" : \
             (self.button1_clicked, arg1,arg2) , ...

dic = { "on_button1_clicked" : \
            self.button1_clicked,
            "on_serverinfo_destroy" : \
            (gtk.mainquit) }
    self.wTree.signal_autoconnect (dic)
    return

#####CALLBACKS
  def button1_clicked(self,widget):
    print "button clicked"

# we start the app like this...
app=appgui()
gtk.mainloop()

It's important to make sure, if you installed pyGTK from source, that you set the PYTHONPATH environment variable to point to /usr/local/lib/python2.2/site-packages/ so pyGTK can be found correctly. Also, make sure you copy project1.glade into your current directory. You should end up with something like Figure 8 when you run your new program. Clicking GO! should produce a nifty button-clicked message in your terminal window.

Figure 8. The Initial Server Info GUI

To make the application actually do something interesting, you need to have some way to determine which host and which port to use. The following code fragment, put into the button1_clicked() function, should do the trick:

host=self.wTree.get_widget("entry1").get_text()
port=int(self.wTree.get_widget(
  "spinbutton1").get_value())
if host=="":
  return
import urllib
page=urllib.urlopen(
  "http://"+host+":"+str(port)+"/")
data=page.read()
print data

Now when GO! is clicked, your program should go off to a remote site, grab a Web page and print the contents on the terminal window. You can spice it up by adding more rows to the hbox and putting other widgets, like a menubar, into the application. You also can experiment with using a table instead of nested hboxes and vboxes for layout, which often creates nicer looking layouts with everything aligned.

TextViews

You don't really want all that text going to the terminal, though, do you? It's likely you want it displayed in another widget or even in another window. To do this in GTK v2, use the TextView and TextBuffer widgets. GTK v1 had an easy-to-understand widget called, simply, GtkText.

Add a TextView to your Glade project and put the results in that window. You'll notice that a scrolledwindow is created to encapsulate it. Add the lines below to your init() to create a TextBuffer and attach it to your TextView. Obviously, one of the advantages of the GTK v2 way of doing things is the two different views can show the same buffer. You also may want to go into the Properties window for scrolledwindow1 and set the size to something larger so you have a decent view space:

self.logwindowview=self.wTree.get_widget("textview1")
self.logwindow=gtk.TextBuffer(None)
self.logwindowview.set_buffer(self.logwindow)

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

glade c

Anonymous's picture

how can be get the previous window on click of a button on current window in glade c???

Visibility

ezander's picture

It seems with glade-3 visibility of GtkWindow is off by default. That means, if you do the steps outlined here and run the program you exactly see nothing. To turn visibility on, select the GtkWindow (serverinfo) in the tree view in glade, select the Common tab, and set Visible to Yes. Trivial, yes, but took me some time to find in this property zoo.

How to connect two windows in pygtk

Vinanti's picture

I am recently working with python. I want to create a GUI where i want to appear one window after clicking on the button of another window. How can I do that?
And after calling one window I want the control should be gone to next window and it shouldn't remain on initial one.
Plz can anybody help me?

How to connect two windows in pygtk

Vinanti's picture

I am recently working with python. I want to create a GUI where i want to appear one window after clicking on the button of another window. How can I do that?
And after calling one window I want the control should be gone to next window and it shouldn't remain on initial one.
Plz can anybody help me?

www.experl.com

www.experl.com's picture

... and you thought this guy _only_ did cutting-edge comsec research?

http://www.experl.com

starting appgui.py

rudbol's picture

rudi@snjo:~$ Prosjekter/prosjekt1/src/appgui.py

(appgui.py:5573): libglade-WARNING **: could not find glade file 'project1.glade'
Traceback (most recent call last):
File "Prosjekter/prosjekt1/src/appgui.py", line 66, in ?
app=appgui()
File "Prosjekter/prosjekt1/src/appgui.py", line 35, in __init__
self.wTree=gtk.glade.XML (gladefile,windowname)
RuntimeError: could not create GladeXML object

check the path of the glade file

Anonymous's picture

same as Subject.

Heh

Tyler Oderkirk's picture

... and you thought this guy _only_ did cutting-edge comsec research?

Code???

Antix's picture

It would be nice to know what to do with the bits of code your pasting in the tutorial.

As it is your pasting the code and saying 'hey, this is a piece of the program, goodluck with it!'

How are beginners like myself supposed to know where to place this code?

Other than that nice tutorial, for as far as I was able to follow it

Im getting Syntax Error

Anonymous's picture

Im getting Syntax Error at
pygtk.require ...

already installed pygtk at ubuntu 6.06, including devel package... :<

ok now... new problem: if i

Anonymous's picture

ok now... new problem: if i try to contact google.com it works, but if i try www.google.com it fails:

GtkWarning: gtk_text_buffer_emit_insert: assertion `g_utf8_validate (text, len, NULL)' failed
self.logwindow.insert_at_cursor(data,len(data))

Same problem, found fix

Anonymous's picture

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

here's the code for the first section with corrected indents and comments:

#!/usr/bin/env python

import sys

try:
    import pygtk
    #tell pyGTK, if possible, that we want GTKv2
    pygtk.require("2.0")
except:
    #Some distributions come with GTK2, but not pyGTK
    pass

try:
    import gtk
    import gtk.glade
except:
    print "You need to install pyGTK or GTKv2 ",
    print "or set your PYTHONPATH correctly."
    print "try: export PYTHONPATH=",
    print "/usr/local/lib/python2.2/site-packages/"
    sys.exit(1)

#now we have both gtk and gtk.glade imported
#Also, we know we are running GTK v2

class appgui:
    def __init__(self):
        """
        In this init we are going to display the main
        serverinfo window
        """
        gladefile="project1.glade"
        windowname="serverinfo"
        self.wTree=gtk.glade.XML (gladefile,windowname)
        # we only have two callbacks to register, but
        # you could register any number, or use a
        # special class that automatically
        # registers all callbacks. If you wanted to pass
        # an argument, you would use a tuple like this:
        # dic = { "on button1_clicked" : 
        #         (self.button1_clicked, arg1,arg2) , ...
    
        dic = { "on_button1_clicked" : 
                self.button1_clicked,
                "on_serverinfo_destroy" : 
                (gtk.mainquit) }
        self.wTree.signal_autoconnect (dic)
        return
  
    #####CALLBACKS
    def button1_clicked(self,widget):
        print "button clicked"

# we start the app like this...
app=appgui()
gtk.mainloop()

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

hmm, the actual post doesn't look like the preview and tags don't work. well here are the major mistakes:

(add a comment) # (self.button1_clicked, arg1,arg2) , ...

(add an indent) dic = { "on_button1_clicked" :

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

Wow, this is awesome!!!

I'm having just a couple problems though.
-How do you add keyboard shortcuts?
-Whenever I run the program from a different directory (i.e. "../file.py") it can't find the Glade file and dies. How do you tell Python to look in the same directory the .py file is in?

Whenever I run the program fr

Anonymous's picture

Whenever I run the program from a different directory (i.e. "../file.py") it can't find the Glade file and dies. How do you tell Python to look in the same directory the .py file is in?

sys.path[0]

is the path of the directory where the python script was found. So you could add (example):

import sys
gladefile = sys.path[0] + "/" + "project1.glade"

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

I fiddled around for some hours, but repeatedly got the following error:

ibglade-CRITICAL **: file glade-xml.c: line 1172 (glade_xml_build_interface): assertion `wid != NULL' failed

I cost me quite some time to find out that the windowname used in your app has to be the same as the one given to the window in glade. Here's a posting on that issue: http://mail.python.org/pipermail/tutor/2003-September/025114.html

Maybe it's worth mentioning, it seems to me like some common pitfall and maybe it saves others time as well as frustration.

So, the following two entries have to be the same:
.glade file:
widget class="GtkWindow" id="window1"
.py file:
windowname="window1"
sebas.

Thanks! I wasted an hour

krizzz's picture

Thanks! I wasted an hour trying to figure this out.

Thank you :-)

Praveen S. Khan's picture

Thank you .... :-)

I was also trapped in that ...

--
psk

libglade-CRITICAL error

sirclif's picture

Thanks a lot, i have been trying to figure out what was causing this error forever. I couldn't find the file glade-xml.c on my system, so i had no idea where to go. thanks again.

Thanks a lot ! I got the sam

Anonymous's picture

Thanks a lot !
I got the same problem and could solve it now

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

Sorry...first time ever with python here, and I can't seem to make this work. When I add the lines

self.logwindowview=self.wTree.get_widget("textview1")
self.logwindow=gtk.TextBuffer(None)
self.logwindowview.set_buffer(self.logwindow)

I get an error when I try to run it:

Traceback (most recent call last):
File "./main.py", line 44, in ?
app=appgui()
File "./main.py", line 30, in __init__
self.logwindowview.set_buffer(self.logwindow)
AttributeError: 'NoneType' object has no attribute 'set_buffer'

The same thing happens when I replace the initialization of logwindowview with self.logwindowview=gtk.glade.XML(gladefile,"textview1"), except instead of "Nonetype object" I get "gtk.glade.XML object" in the error. Am I missing something?

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

me too. i stopped at that point.

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

nevermind....apparently Glade didn't believe me when I said "Save." Problem fixed!

Speed ??

Anonymous's picture

I am always curious, how fast is it in the end compared to c/gtk+ or tcl/tk or tcl/gtk+ ?

Re: Speed ??

Anonymous's picture

I don't know for sure, but just a guess there's probably not a very big difference since all the drawing and socket handling is still in C somewhere under all the Python code; I wouldn't imagine this particular app to be very different from it's C counterpart.

Re: Speed ??

Anonymous's picture

The answer is "Fast Enough."

I don't notice the difference between pyGTK and a normal GTK app. Obviously it doens't load up quite as quickly, since it has to load all of Python, but on a reasonably fast machine, you can't tell.

Dave Aitel
Immunity, Inc.

explanation confuses vertical and horizontal boxes

Anonymous's picture

The text confuses vertical and horizontal boxes. This is probably obvious, but I don't want anyone to be confused when the text says horizontal box when referring to figure 2, when it is in fact a vertical box. The text for figure 3 says vertical box but should say horizontal box.

Re: explanation confuses vertical and horizontal boxes

Anonymous's picture

I was wondering about that!

MacOS

Anonymous's picture

What about MacOs X?

Re: MacOS

Anonymous's picture

as long as you have gtk and python installed (via fink or from source) and some sort of X server, it shouldn't be a problem

Re: MacOS

Anonymous's picture

I've seen various GTK+ progs on Mac OS X, I'm sure it's possible.

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

I was just discovering pyGtk and marvelous pyGlade, this quite complete and simple introduction comes at the right time ! (I just found a pyGlade tutorial which was too simple and written for glade1, and pyGtk faq is quite chaotic (that's also fun))
I like the eleguance of such a solution.
I would be interested in knowing how to pack a python application in a win32 installer.
thanks.
Alex

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

Greate article, I was just about to start writing a ton of threading code since I had never heard of input_add(), thanks for saving me the trouble.

I think the time for Python based GTK2 apps is now. Two, or even one, year ago I wouldn't have chosen Python for my project because the dependencies were too many. But since Red Hat now codes all their configuration utilities with pyGTK2 I am confident that many will already have all the libraries installed and that the language bindings will continue to be maintained. If distributed properly there is no difference between a GTK app done in C or python from the end users point of view, as it should be.

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

I'm getting an error when running.
First I think the following line should have been commented out.
(self.button1_clicked, arg1,arg2) , ...
Second I'm getting an the following error.
File "./serverinfo.py", line 47
self.wTree.signal_autoconnect (dic)
^
SyntaxError: invalid syntax

Any Ideas, at first I typed everything in. Thinking there must have been a typo somewhere, I copied and pasted from the site and still get that error.

Thanks,

John R. Tipton

I had a problem like this...

Anonymous's picture

I had a problem like this and it was that I didn't finsh the dictionary on the line before off with a '}' . Maybe that's your problem.

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

remember: indention matters in python.
the lines
dict = {...
self.wTree.signal_autoconnect (dic)

should be indented same as the

self.wTree=gtk.glade.XML (gladefile,windowname)
before

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

Another option is to get the .tgz file from the ftp site mentioned at the end.
The code in the article (both online and in print) has been through some
sort of formatter which seems to not like Python..

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

So how involved is it to roll out python, pygtk, gtk2 and your application for clients who use windows and have none of that installed? Is it a case of it being better for you to do it yourself or is there a neat automated way one could create an "installer"? I was going to use python + pygame for a recent project, but ended up doing it in C for ease of distribution (windows ppl just download a binary and run it).

Nick

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

Personally, I make my clients "Do it themselves". This currently involves downloading three executables from public websites, and double-clicking on each of them in turn.

In other words, it is extremely easy. It does take a few megs, but we've all got a few megs to spare for Python, right?

There is an annoying gotcha or two with Win32, of course. My current favorite is that Win32 will let you listen on one port twice in a row. Aside from that minor issue though, the Win32 CANVAS (which is what the article is based on) runs exactly the same as the Linux CANVAS. This was an impotant design consideration for me when I started the project.

Dave Aitel
Immunity, Inc.

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

"just for next time". Pygame games are easily turned into standalone binaries. If you use one of the free "installer" programs, no one will ever know it was written in python. There are already "shareware" style games written in python for windows users. examples? click and play!

SolarWolf (my humble game, exe installer)
http://www.pygame.org/shredwheat/solarwolf/

BaseGolf (a shareware one, exe installer)
http://alitius.com/games/basegolf/

Pathological (new retro puzzler, zipped exe)
http://pathological.sourceforge.net/

These were all done using the "py2exe" tool and various packaging/installer tools.

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

Wow! They must have used a time machine to travel to the future to post this!

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

Their dates have been wrong for years. They don't seem to care.

I thought it was funny the first time I posted the same crack, but everybody else ignored me, and I nobody's ever fixed it.

Anyways, welcome to Linux Journal, dude.

~Bitterman~

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

This is a sample article from the September issue, so its official date is September 1. Magazines generally put out a given month's issue during the previous month. Why? Tradition.

And yes, we're going to publish the Linux drivers for the time machine.

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

If you're considering writing an app. for GTK+ 1.2, keep in mind that the Python bindings for GTK+ 2 are actively maintained. If you encounter a bug in the old bindings, of which I'm sure there are a few, they will not likely be fixed.

A cool feature of the latest PyGTK release, 1.99.17, is support for custom widgets. GLADE's widget palette isn't complete, so you can add a placeholder with the name of a widget creation function. I use this to embed gnome-terminal's VTE terminal emulator widget.

You'll notice that GLADE's signal handler drop-down list contains the names of C functions, like gtk_widget_show(). You can take advantage of this by writing Python functions with the same name. I frequently use the following functions:

  • gtk_main_quit
  • gtk_true
  • gtk_widget_hide
  • gtk_widget_hide_on_delete
  • gtk_widget_show
  • gtk_window_present

Define these before the call to signal_autoconnect() function and you can pass it the value of the built-in locals() function. No need to write your own mapping dictionary.

PyGTK is good stuff. If you've ever seen a GTK+ program written in C, you'll appreciate how elegant a comparable Python can be.

embed terminal in GTK window

Anonymous's picture

You mention that

"A cool feature of the latest PyGTK release, 1.99.17, is support for custom widgets. GLADE's widget palette isn't complete, so you can add a placeholder with the name of a widget creation function. I use this to embed gnome-terminal's VTE terminal emulator widget."

I have tried finding system documentation as well as googled for implementations of python-vte and the use of vte_terminal_new() but there are no examples at the time of writing.

Please elaborate with a brief example of how to embed a terminal in a GTK window.

Any chance of elaborating

Anonymous's picture

Any chance of elaborating with a quick example of the glade file and initial python code.

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

Can anyone can give an example of how to create a new widget using
custom widget

Re: A Beginner's Guide to Using pyGTK and Glade

Anonymous's picture

One clarification regarding signal_autoconnect(): if you write straight-line procedural code, as you are likely to do when starting out, you can define your handler functions at the top level and pass locals() to signal_connect(). For example:

  • def on_button1_clicked( button, *args ):
    • ...
  • gtk.glade.XML( 'project1.glade' ).signal_autoconnect( locals() )

If your handlers are in a class, as in this article, you can pass self to signal_autoconnect(). For example:

  • class appgui:
    • def __init__( self ):
      • ...
      • self.wTree = gtk.glade.XML( 'project1.glade' )
      • self.wTree.signal_autoconnect( self )
    • def on_button1_clicked( button, *args ):
      • ...
White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState