pygtkconsole

(Excuse the quick notes)

I modified the pygtkconsole.py file quickly, cleaning up and changing input refs.

(2011/04/13~22:15) I don’t have any more time right now, but I’ll try to finish everything tomorrow and possible put some additional intelligence into the module. Then I can do whatever I should for the community if/when applicable.


So far only tested on:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 10.10
Release: 10.10
Codename: maverick

Thread @ Ubuntu Forums: pygtkconsole.py program does not accept any input in ubuntu

The interactive console first tutorial run fine. I ran the modified pygtkconsole (below) with: python -i -v -t pygtkconsole.py | tee error.log


>>>
>>> w=Window()
>>> b=Button('Hello')
>>> w.add(b)
>>> def hello(b):
... print "Hello, World!"
...
>>> b.connect('clicked', hello)
13L
>>> w.show_all()
>>> Hello, World!
Hello, World!
Hello, World!

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

I will update upload the code after I test and document. For now, here is is in a block (though my wordpress is killing it – best to grab if in dire need at the Forum link above, sorry).


#!/usr/bin/env python
# -*- Mode: python; c-basic-offset: 4 -*-
#
# Interactive PyGtk Console, Johan Dahlin 2002 with small (and possibly larger) tweaks based on newer dists...
#
# from pygtkconsole

import os
import signal
import sys
import string
import socket, select
from code import InteractiveInterpreter, InteractiveConsole
import gtk
import gobject

# For compatibility, instead of using GDK.INPUT_READ or
# gtk.gdk.INPUT_READ depending on the PyGtk version
# GDK_INPUT_READ = 1
gtk.gdk.INPUT_READ = 1

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, gtk.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('%c%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):
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)

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]
pygtk_version = string.join(map(str, gtk.pygtk_version), '.')
gtk_version = string.join(map(str, gtk.gtk_version), '.')
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()

FacebookTwitterGoogle+LinkedInPinterestTumblrEvernoteWordPressShare

One thought on “pygtkconsole”

Leave a Reply

Eclectic.1