<br><br><div class="gmail_quote">2009/2/26 Fernando Perez <span dir="ltr"><<a href="http://fperez.net">fperez.net</a>@<a href="http://gmail.com">gmail.com</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi Gustavo,<br>
<div class="Ih2E3d"><br>
On Tue, Feb 24, 2009 at 6:50 AM, Gustavo Carneiro <<a href="mailto:gjcarneiro@gmail.com">gjcarneiro@gmail.com</a>> wrote:<br>
> Hello, I have been having a problem since upgrading IPython. I don't<br>
> remember exactly which version I had before, but now it's 0.8.4.<br>
><br>
> Consider the following script:<br>
><br>
> import gtk<br>
><br>
> win = gtk.Window()<br>
> win.show()<br>
> bt = gtk.Button("Hello")<br>
> bt.show()<br>
> win.add(bt)<br>
><br>
> print "begin"<br>
> gtk.main()<br>
> print "end"<br>
><br>
><br>
> In IPython 0.8.4, the gtk.main() call will immediately return and IPython<br>
> re-runs a new gtk.main(), possibly in a different thread. In a previous<br>
> version (sorry, don't remember which) my own gtk.main() call would not exit,<br>
> it would just run the GUI. I was sure that, when gtk.main() returned it was<br>
> because the window had been closed.<br>
><br>
> Now I am unsure how to make it work again. I have module function that is<br>
> supposed to "run the GUI and don't return until everything is finished".<br>
> Any advice on how to accomplish this without breaking the simple module API<br>
> that I provide? Is there something like,<br>
> IPython.wait_until_gui_is_finished() ?<br>
<br>
</div>Could you try running the attached script the way you used 'ipython<br>
-gthread' before? This would help us understand the problem better,<br>
if this works the way you expect it.</blockquote><div><br>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:<br>
<br></div></div><div style="margin-left: 40px;"><span style="font-family: courier new,monospace;"># My GUI code</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import gtk</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">w = gtk.Window()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">b = gtk.Button("Hello")</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">b.show()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">w.add(b)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">w.show()</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># Run the gtk main loop, with IPython if possible</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">try:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> import IPython.Shell</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">except ImportError:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> gtk.main()</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">else:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def hijack_gtk():</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> """No-op version of the original mainloop hijacker</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> """ </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> import gtk</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return gtk.main</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> IPython.Shell.hijack_gtk = hijack_gtk</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ip = IPython.Shell.IPShellGTK()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ip.mainloop()</span><br></div>
<br>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.<br><br>Maybe if the "hijacked gtk main loop" was the code below my comment "<span style="font-family: courier new,monospace;"># Run the gtk main loop, with IPython if possible"</span>, 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.<br clear="all">
<br>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. :-)<br><br>Anyway, thanks for your answer, it has been very useful.<br>
<br>[1] <a href="http://ipython.scipy.org/moin/Cookbook/EmbeddingInGTK">http://ipython.scipy.org/moin/Cookbook/EmbeddingInGTK</a><br><br>-- <br>Gustavo J. A. M. Carneiro<br>INESC Porto, Telecommunications and Multimedia Unit<br>
"The universe is always one step beyond logic." -- Frank Herbert<br>