[pygtk] Python code works in terminal but not with shortcut on the gnome panal

Felix Rabe (public) public at felixrabe.textdriven.com
Fri Mar 30 08:09:24 WST 2007


Edward A Robinson wrote:
> Any Ideas on how to debug code without the terminal, the problem is
> that it only happens without the terminal so I have no real idea how
> to debug it.

You may want to skip to "AD" now if you'd like to try out a free 
software interactive PyGTK RAD tool for debugging.


Debugging without a terminal was my point actually (I've read your 
message), since your try: ... statement thingie would now look like this:

try:
    url_content = urllib.urlopen('http://www.google.com')
    main_window.add(gtk.Label(url_content.read()))
except IOError:
    main_window.add(gtk.Label('             Error            '))
except:  # NEW!
    main_window.add(gtk.Label('Error: unexpected exception')  # NEW!

You can also use the traceback module, optionally do 
label.modify_font(pango.FontDescription("monospace")), and "print" your 
traceback to the label after putting it into a gtk.ScrolledWindow() (!).

Or yes, just redirect stderr as in the other email.  I prefer my nifty 
tool though :) - PyGTKShell:

======

"AD"

You can get my PyGTKShell from http://felixrabe.net/pygtk-shell/ (just 
exctract the archive and run pygtk_shell.py either from a terminal or 
the panel) and try out urllib "live" in PyGTK.  Running that shell from 
the panel should give you the Python prompt in the same environment as 
running your application from the panel.

Use the File>>Text Editor to enter whole scripts and execute them 
(Ctrl-E if you don't want to hit the button all the time) - stdout and 
stderr goes to the Console window where you opened the Text Editor from.

Your script would now look like this (rewritten to use my interactive 
GTK classes and decoding the latin-1 coding):

======================================================================
import urllib

main_window = Window()

try:
     url_content = urllib.urlopen('http://www.google.com')
     unicode_string = url_content.read().decode("latin-1")
     main_window(ScrolledWindow(Label(unicode_string)))
except IOError:
     main_window(Label('             Error            '))
except:
     main_window(Label('Unexpected exception'))
======================================================================

Or even much better just: (because you'll see all output anyway)

======================================================================
import urllib
url_content = urllib.urlopen('http://www.google.com')
print url_content.read().decode("latin-1")
======================================================================

(By the way, that script takes a long while to execute on my machine 
because lookup outside Mozilla programs is generally very slow.)


PyGTKShell is made and optimized for interactive RAD and to manually try 
out and debug small pieces of code like yours.  I hope you enjoy it. 
I'm working on a rewrite (due before Christmas I hope) based on the 
newest GEdit Python Console plugin.  Feedback of any kind is welcome and 
I'll do my best to answer friendly to make up for the bad to 
non-existant docs.  :-)

Greetings,
- Felix


More information about the pygtk mailing list