SourceFiles.org - Use the Source, Luke
Home | Register | News | Forums | Guide | MyLinks | Bookmark

Sponsored Links

Latest News
  General News
  Reviews
  Press Releases
  Software
  Hardware
  Security
  Tutorials
  Off Topic


Back to files

PyGTK Shell 0.1.0

The PyGTK Shell is a framework making Python applications open to realtime programmatic intervention by its users.

This is just the right tool for Python-programming control freaks.

Principal author

Felix Rabe <public@felixrabe.textdriven.com>

Please send feedback.

Requirements


Python 2.4.0        -- http://www.python.org/
                   or: http://www.activestate.com/Products/ActivePython/

GTK 2.8.0           -- http://www.gtk.org/
            for win32: http://gimp-win.sourceforge.net/
PyGTK 2.8.0         -- http://www.pygtk.org/

PyWin32 (on win32) -- http://sourceforge.net/projects/pywin32/

(only needed on MS Windows; included in ActivePython)

Installation


To install, just (view and) extract the archive somewhere. Stay in the created directory for experiments - there is no need to further install anything.

Tutorial (outdated; hint: some stuff moved to wid.shell[0])


Starting out

Start the shell from its directory:

$ ./shell.pyw (the .py extension might work better on win32)

You will get a window with the current directory, an empty output area, and tabs (Python, System, ChDir) providing different ways to enter commands.

Explore the Python environment by entering the following commands:

>>> dir()
['Output', 'ShellWindow', 'Structure', 'TextEditor', 'TextEditorWindow', 'Window', '__builtins__', 'cairo', 'cwd_changer', 'cwd_display', 'd', 'gobject', 'gtk', 'namespace', 'os', 'output', 'pango', 'python', 'sys', 'system', 'window']

Ok, this is boring. Hit the "Up" key and find out that there's a history.

>>> dir()
['Output', 'ShellWindow', 'Structure', 'TextEditor', 'TextEditorWindow', 'Window', '__builtins__', 'cairo', 'cwd_changer', 'cwd_display', 'd', 'gobject', 'gtk', 'namespace', 'os', 'output', 'pango', 'python', 'sys', 'system', 'window']

At some point, also try out the Shift-Return combination while browsing the history.

You can enter multi-line statements naturally:

>>> for i in range(10):
... print i
...
0
1
2
3
4
5
6
7
8
9

This way, you can fill up the output area pretty quickly. To clean it:

>>> output.clear_text()

I'm bored

Create a window! The Window class (module pygsh.window) is derived from gtk.Window, you can use it to create a second (empty) PyGTK shell window:

>>> win = Window()
>>> win.set_keep_above(True) # this is especially neat on win32... >>> win.set_title("The Application Window With The Most Features Ever")

Pretty cool, eh? But still empty...

>>> text = win(TextEditor())

Now that's MUCH less boring - you practically wrote your own text editor! By the way, the Window class has wonderful fullscreen support built in - just hit F11 twice.

There's a specialized Window subclass for just the above:

>>> win2 = TextEditorWindow()

This is still a very simple text editing widget, it does not yet implement undo, key strokes (like Ctrl-S, but check out Shift-Tab), file overwriting confirmation, syntax highlighting. For now, I leave this all as an exercise to the reader.

Remove the win2 window:

>>> win2.destroy()

Keep the first window, or re-enter the statements above to create it. Pressing Shift-Return in the history supports you.

Text editing

The System and ChDir tabs provide system access - on the first, you can execute shell commands, on the second you can change the current directory (this does have tab completion). Hint: Ctrl+Page Up/Down lets you change those contexts quickly without losing focus. (That's provided by gtk.Notebook.)

But before going there, enter another Python command:

>>> system.output = text

This will make all the system commands' output go to the new window.

Try it out! On the System prompt, enter the following command on Unix:

$ ls -al

On Windows:

$ dir

You can now mess around with the text editor and save your directory listing wherever you want.

Text execution

You can also use the text editor to execute Python code. For this, first close all windows with a TextEditor in them. Then, at the Python prompt, type:

>>> TextEditorWindow(python)

You will (hopefully) notice the new "Execute" button in the Text Editor's toolbar. Just enter a simple Python statement and hit "Execute".

Changing directories

Let's do something about that "Current directory" display! The Python way to change the directory is:

>>> os.chdir("..")

This is too cumbersome, there should be a quicker way. By definition, a directory change is confined to the process where this happens. You can't just let a subprocess execute a "cd some/where/i/like" command and expect it to work like you want. But at the System prompt try the following:

$ [Hit the "C" key and watch out] [Hit the "D" key] [Hit "Space"]

You're not on the System prompt anymore! The ChDir prompt lets you change the directory fairly easily (it features tab completion). When you're finished, the ChDir prompt will pass the control back to the System prompt. Whitespace is ignored as long as there is no text since on terminals it's common (in the Unix sh shell, necessary) to hit the space key after the "cd" command.

One easy way to change the directory is to use the two buttons to its right. Clicking on ".." changes to the parent directory, clicking on "cd" opens up a dialog to choose a directory from.

Going home

You can close all the windows one by one - the last one closed will exit the PyGTK Shell. You can also close a window by typing Control-D if it contains a focused Python / System / ChDir prompt. To close it all at once, on the Python prompt, type:

>>> sys.exit()

The Future

See TODO.txt and ROADMAP.txt and help implement the features by sending patches!


Sponsored Links

Discussion Groups
  Beginners
  Distributions
  Networking / Security
  Software
  PDAs

About | FAQ | Privacy | Awards | Contact
Comments to the webmaster are welcome.
Copyright 2006 Sourcefiles.org All rights reserved.