This programm works as a extraction of the normal initialization of modules plus the creation of each GTK widget and the signal interface. What this console do is basically simplify the coding of small GUI to a great extend. I personally found it similar to the wxPython syntax.

To do this example you will need to have the python-gtk+2.0 package. Then download the pygtkconsole from here and finally execute it into the interactive shell.

  Interactive console to manipulate GTK+ widgets.
  >>> w=Window()
  >>> b=Button('Hello')
  >>> w.add(b)
  >>> def hello(b):
  ...     print "Hello, World!"
  >>> b.connect('clicked', hello)
  >>> w.show_all()
  >>> Hello, World!
  Hello, World!
  Hello, World!

  >>> b.set_label("Hi There")

We create a window with a widget. The last line even go as far as dynamically modifying the label of the button. So we can send singals on the dynamic shell and see the label hello inputed on the line "" b.connect('clicked', hello) to "Hi there".

The source of the pygtkconsole is not that hard and contains less than 100 lines of code. It can be divided into 3 main parts:

   * Importing the necessary modules
   * The interactive interpreter
   * Connecting the signals to the GTK instruction
import os
import signal
import sys
import string
import socket, select
from code import InteractiveInterpreter, InteractiveConsole

import gtk
import gobject

We used some nifty modules suhch as signal, sys and os for setting our groundbase with the creation of the system console. Then a module to interact between the system and the interactive console and finally signals, sockets and select for the type of connectivity this instructions will be connecting to the GUI module.

Finally the module GTK and gobject buts the GUI toolkit at the mercy of python.

The second one is the logic in which the interactive console with connect with the system.

 class Mainloop(InteractiveInterpreter):
    def __init__(self, read_fd, sock):
        self._rfd = os.fdopen(read_fd, 'r')
        self._sock = sock
        gobject.io_add_watch(read_fd, GDK_INPUT_READ, self.input_func)

    def read_message(self):
        length = ord(

    def input_func(self, fd, cond):
        data = self.read_message()
        more = self.runsource(data)
        return True

    def run(self):

class Console(InteractiveConsole):
    def __init__(self, write_fd, sock, pid):
        self._wfd = os.fdopen(write_fd, 'w')
        self._sock = sock = pid

    def send_message(self, message):
        self._wfd.write('%c%s' % (len(message), message))

    def interact(self, banner=None):
        InteractiveConsole.interact(self, banner)
        # Die child die
        os.kill(, 9)

    def runsource(self, source, filename):
        # wait for notification from parent[self._sock],[],[])
        more = ord(self._sock.recv(1))
        return more

class GtkInterpreter(Console):
    def __init__(self):
        rfd, wfd = os.pipe()
        # set up socket for returning command result
        sigsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock_addr = None
        for port in range(4321,5321):
                sigsock.bind(('', port))
                sock_addr = ('', port)
        if not sock_addr:
            print "Can't open socket"

        parent_pid = os.getpid()
        child_pid = os.fork()
        if not child_pid:
            # connect to command return socket
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            g = Mainloop(rfd, sock)
            # Wait for command return socket connection
            sock, addr = sigsock.accept()
            Console.__init__(self, wfd, sock, child_pid) 

Finally there is the Importation of the GTK gui and how it's managed by python.

 def interact():
        import readline
        import rlcompleter
        readline.parse_and_bind('tab: complete')
    except ImportError:

    gi = GtkInterpreter()
    gi.push("from gtk import *")

    python_version = string.split(sys.version)[0]
	pygtk_version = string.join(map(str, gtk.pygtk_version), '.')
	gtk_version = string.join(map(str, gtk.gtk_version), '.')
	pygtk_version = '0.6.x'
	gtk_version = '1.2.x'

    banner = """Python %s, PyGTK %s (Gtk+ %s)
Interactive console to manipulate GTK+ widgets.""" % (python_version,

if __name__ == '__main__':