[IPython-user] ipython -gthread behaviour is now to quit the script gtk.main() loop?

Gustavo Carneiro gjcarneiro@gmail....
Thu Feb 26 04:39:26 CST 2009

2009/2/26 Fernando Perez <fperez.net@gmail.com>

> Hi Gustavo,
> On Tue, Feb 24, 2009 at 6:50 AM, Gustavo Carneiro <gjcarneiro@gmail.com>
> wrote:
> > Hello, I have been having a problem since upgrading IPython.  I don't
> > remember exactly which version I had before, but now it's 0.8.4.
> >
> > Consider the following script:
> >
> > import gtk
> >
> > win = gtk.Window()
> > win.show()
> > bt = gtk.Button("Hello")
> > bt.show()
> > win.add(bt)
> >
> > print "begin"
> > gtk.main()
> > print "end"
> >
> >
> > In IPython 0.8.4, the gtk.main() call will immediately return and IPython
> > re-runs a new gtk.main(), possibly in a different thread.  In a previous
> > version (sorry, don't remember which) my own gtk.main() call would not
> exit,
> > it would just run the GUI.  I was sure that, when gtk.main() returned it
> was
> > because the window had been closed.
> >
> > Now I am unsure how to make it work again.  I have module function that
> is
> > supposed to "run the GUI and don't return until everything is finished".
> > Any advice on how to accomplish this without breaking the simple module
> > that I provide?  Is there something like,
> > IPython.wait_until_gui_is_finished() ?
> Could you try running the attached script the way you used 'ipython
> -gthread' before?  This would help us understand the problem better,
> if this works the way you expect it.

Yeah, more or less.  I have to change slightly my strategy.  I cannot run
the simulation with ipython -gthread; instead I just run with plain old
python and import IPython module and run the main loop.  Taking your code I
get this:

# My GUI code
import gtk
w = gtk.Window()
b = gtk.Button("Hello")

# Run the gtk main loop, with IPython if possible
    import IPython.Shell
except ImportError:
    def hijack_gtk():
        """No-op version of the original mainloop hijacker
        import gtk
        return gtk.main

    IPython.Shell.hijack_gtk = hijack_gtk

    ip = IPython.Shell.IPShellGTK()

This works perfectly.  It is not as "transparent" as before (you could run
ipython -gthread on a unmodified pygtk program), but it does the job nicely.

Maybe if the "hijacked gtk main loop" was the code below my comment "# Run
the gtk main loop, with IPython if possible", then things might be more
transparent for pygtk apps.  Because making a gtk.main() a lambda function
that just returns breaks the "blocking main loop" semantics, with possibly
disastrous results.

In any case, this provided the needed motivation for me to add a GUI console
to my pygtk app, following the recipe at [1], which was painless and works
great. :-)

Anyway, thanks for your answer, it has been very useful.

[1] http://ipython.scipy.org/moin/Cookbook/EmbeddingInGTK

Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ipython.scipy.org/pipermail/ipython-user/attachments/20090226/020263d0/attachment.html 

More information about the IPython-user mailing list