Threading with PyGTK and Glade

I find out just one pratical sample about how to use pygtk with thread at the http://pygtk.org/ faq. Although I think that coolest thing of pygtk is use with glade. With no samples available I’d tried to do by myself. I am glad because at the end it was pretty much the same stuff as the sample that wasn’t using glade! Good job done, folks!

  #!/usr/bin/env python
# gui.py, sample of PyGTK + GLADE + Threads use.
# Licence: GPLv2.0

    import pygtk
    print "You need to install pyGTK or GTKv2 or set PYTHONPATH."

import sys
import gtk
import gtk.glade
import gobject
import threading
import time

class Engine:
    def __init__(self):

    def init_app(self):
        print "Engine Start!"
        print "Engine End!"


class AppGui(threading.Thread):

    def __init__(self):
        super(AppGui, self).__init__()

    def init_app(self):
        self.gladefile = "gui.glade"
        self.wTree = gtk.glade.XML(self.gladefile)
        dic = { "on_button1_clicked"    : self.run,
                "on_MainWindow_destroy" : gtk.main_quit }

    def run_back(self, value):
        print value
        self.engine = Engine()

    def run(self, widget):
        value = "some value..."
        print "Before!"
        gobject.idle_add(self.run_back, value)
        ## Comment line above an try line bellow!
        # self.run_back(value)
        print "After!"

if __name__ == "__main__":
    app = AppGui()

Bellow we got the “gui.glade” file. It will be wrapped by “gui.py” above, so put it in the same directory before run.

<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">


<widget class="GtkWindow" id="window1">
<property name="visible">True</property>
<property name="title" translatable="yes">window1</property>
<property name="type">GTK_WINDOW_TOPLEVEL</property>
<property name="window_position">GTK_WIN_POS_NONE</property>
<property name="modal">False</property>
<property name="resizable">True</property>
<property name="destroy_with_parent">False</property>
<property name="decorated">True</property>
<property name="skip_taskbar_hint">False</property>
<property name="skip_pager_hint">False</property>
<property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
<property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
<property name="focus_on_map">True</property>
<property name="urgency_hint">False</property>

<widget class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>

<widget class="GtkButton" id="button1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">   Start
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button1_clicked"
last_modification_time="Wed, 07 Mar 2007 18:35:44 GMT"/>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>


And It works, see by yourself. Although I’m still looking for others threading samples to user interfaces. Any sugestion?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


"Don't worry about what anybody else is going to do. The best way to predict the future is to invent it. Really smart people with reasonable funding can do just about anything that doesn't violate too many of Newton's Laws!" -- Alan Kay in 1971

My Twitter

My Delicious

RSS My Shared RSS

  • An error has occurred; the feed is probably down. Try again later.

Blog Stats

  • 78,147 hits

%d bloggers like this: