Py_Shell
Py_Shell is a module that I wrote for the gnominide project, but it can be useful also as a
standalone program, or integrated in other projects.
It is basically a Python interpreter inside a GTK+2 gui; you can send commands to the interpreter
and see the output or errors. It has also autocompletion capability; you can start write a word
then press Ctrl+<space> and a nice popup window will appear with the options.
An image is better than many words:
You have also the possibilities to save the output of the shell in a file or save only the commands
you have given. In the same way you can clear the window or reset the interpreter on the fly.
Download PyShell
How to use it as a standalone program
It is really simple; just type into a terminal
$ python Py_Shell.py
How to use it as a debug console in another program
This use of the shell was generated by a discussion in the pygtk mailing list (thanks guys for
the tips). Suppose you have a complex application with a gtk gui, and you want to manipulate or
debug
your app at runtime. Now I will show you how to obtain this with Py_Shell. Let's start with a
minimal example
Suppose you have this file "my_app.py"
#
# my_app.py
#
import pygtk
pygtk.require('2.0')
import gtk
class App:
def __init__(self):
#a list of number
self.list=[1,34,23,8,90]
#the gui
self.win = gtk.Window()
self.label = gtk.Label("Hello world!")
self.win.add(self.label)
self.win.connect("destroy", self.quit)
self.win.set_size_request(300,260)
self.win.show_all()
def quit(self, *widget):
gtk.mainquit()
if __name__=='__main__':
app=App()
gtk.mainloop()
This program simply creates an instance of the App class; App class has three fields: list that is
a Python List object, win a gtkWindow and label a gtkLabel that is added to the window. Now to use
the Py_Shell with this application you do as follow:
-import the Py_Shell module
-create a Py_Shell with the locals of your application
The modified version of the code is:
#
# my_app.py
#
import pygtk
pygtk.require('2.0')
import gtk
import Py_Shell #<---- import Py_Shell
class App:
def __init__(self):
#a list of number
self.list=[1,34,23,8,90]
#the gui
self.win = gtk.Window()
self.label = gtk.Label("Hello world!")
self.win.add(self.label)
self.win.connect("destroy", self.quit)
self.win.set_size_request(300,260)
self.win.show_all()
local = locals() #<--- obtain the locals
shell=Py_Shell.Shell_Gui(Local=local) #<--- create a Py_Shell
def quit(self, *widget):
gtk.mainquit()
if __name__=='__main__':
app=App()
gtk.mainloop()
Now start the app (make sure Py_Shell is in you pythonpath); near your window you will see also a
Py_Shell. Try this command into the shell
dir()
you will obtain:
['__builtins__', 'self']
'self' is the instance of your app; write this into the shell:
self.label.set_text(" Ciao! ")
self.set_use_markup(1)
Have you noticed any difference ? :-) You are manipulating you app at runtime! And this is true
also for non gui element; try :
self.list="now list is a string"
As you can image there are many others possibilities to use the shell with more complex
applications; try all of them!
In the above example we have seen only one argument for the Py_Shell constructor, but there are others arguments:
Shell_gui(with_window=1,banner=BANNER, label_text="Interactive Python Shell", Local=None )
with_window specify if the shell sould be into its own window or not. In the second case the
constructor will return a gtk.Frame and you can put it inside you app
banner is the text displayed at the begin of the textView
label_text is the text title of the frame
To clarify these arguments, replace the line
shell=Py_Shell.Shell_Gui(Local=local)
with
shell=Py_Shell.Shell_Gui(banner="Give me some commands", label_text="Debug Shell", Local=local)
and look at the differences