Pygtkconsole

Main.Pygtkconsole History

Hide minor edits - Show changes to markup

May 18, 2007, at 03:48 PM EST by JZA -
Changed line 3 from:

To do this example you will need to have the python-gtk+2.0 package. Then download the pygtkconsole from [http://www.pygtk.org/pygtk2tutorial/examples/pygtkconsole.py here] and finally execute it into the interactive shell.

to:

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.

May 18, 2007, at 03:47 PM EST by JZA -
Changed lines 117-118 from:

<<<<<<<

to:
Deleted lines 144-173:
    interact() @]

=======

Finally there is the Importation of the GTK gui and how it's managed by python. [@ def interact():

    try:
        import readline
        import rlcompleter
        readline.parse_and_bind('tab: complete')
    except ImportError:
        pass

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

    python_version = string.split(sys.version)[0]
    try:
	pygtk_version = string.join(map(str, gtk.pygtk_version), '.')
	gtk_version = string.join(map(str, gtk.gtk_version), '.')
    except:
	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,

       pygtk_version,
       gtk_version)
    gi.interact(banner)

if __name__ == _main_:

May 18, 2007, at 03:46 PM EST by JZA -
Changed lines 117-118 from:
to:

<<<<<<<

Changed lines 146-176 from:
    interact() @[
to:
    interact() @]

=======

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

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

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

    python_version = string.split(sys.version)[0]
    try:
	pygtk_version = string.join(map(str, gtk.pygtk_version), '.')
	gtk_version = string.join(map(str, gtk.gtk_version), '.')
    except:
	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,
       pygtk_version,
       gtk_version)
    gi.interact(banner)

if __name__ == '__main__':
    interact() 
May 18, 2007, at 03:44 PM EST by JZA -
Changed lines 24-26 from:

The source of the pygtkconsole is not that hard and contains less than 100 lines of code.

[@ class GtkInterpreter(Console):

to:

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):
        InteractiveInterpreter.__init__(self)
        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(self._rfd.read(1))
        return self._rfd.read(length)

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

    def run(self):
        gtk.main()

class Console(InteractiveConsole):

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

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

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

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

class GtkInterpreter(Console):

Added lines 117-145:

Finally there is the Importation of the GTK gui and how it's managed by python. [@ def interact():

    try:
        import readline
        import rlcompleter
        readline.parse_and_bind('tab: complete')
    except ImportError:
        pass

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

    python_version = string.split(sys.version)[0]
    try:
	pygtk_version = string.join(map(str, gtk.pygtk_version), '.')
	gtk_version = string.join(map(str, gtk.gtk_version), '.')
    except:
	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,

       pygtk_version,
       gtk_version)
    gi.interact(banner)

if __name__ == _main_:

    interact() @[
May 18, 2007, at 03:34 PM EST by JZA -
May 18, 2007, at 03:34 PM EST by JZA -
May 18, 2007, at 03:34 PM EST by JZA -
Changed line 4 from:
to:

http://www.pygtk.org/pygtk2tutorial/figures/helloworld.png

Changed lines 22-53 from:

We create a window with a widget. The last line even go as far as dynamically modifying the label of the button.

to:

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.

 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):
            try:
                sigsock.bind(('', port))
                sock_addr = ('', port)
            except:
                pass
        if not sock_addr:
            print "Can't open socket"
        sigsock.listen(1)

        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)
            sock.connect(sock_addr)
            g = Mainloop(rfd, sock)
            g.run()
        else:
            # Wait for command return socket connection
            sock, addr = sigsock.accept()
            Console.__init__(self, wfd, sock, child_pid) 
May 18, 2007, at 03:29 PM EST by JZA -
Added lines 1-22:

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 [http://www.pygtk.org/pygtk2tutorial/examples/pygtkconsole.py 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)
  5
  >>> 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.