matplotlib in GTK3 - interactive window while not calling Gtk.main()

Dear Members, Recently I upgraded to from matplotlib 1.4.3 + gtk2 to matplotlib 2.2.2 + gtk3 and I have a problems with porting my old code. I'm using matplotlib in Gtk window with typical: win = Gtk.window() vbox = Gtk.Box(...) win.add(vbox) fig = pylab.figure() canvas = FigureCanvas(fig) toolbar = NavigationToolbar(canvas, fig) axis = fig.add_axes(...) vbox.pack_start(canvas,...) vbox.pack_start(toolbar,....) win.show_all() while(Gtk.events_pending()): Gtk.main_iteration() After this code I have a valid window on screen however it does not reacts on mouse clicks (zooming etc.) until calling Gtk.main_iteration() in loop or Gtk.main(). I can understand why, however it was working differently with old matplotlib and gtk2 (window was responsive all time). The rest of my code is a python "Text User Interface" application (controlled by commands typed to terminal), the window is only for showing the results (animated waveform). Now I would like to keep the application as it is, but also to have responsive matplotlib window, without calling Gtk.main(). Is it possible? If yes, then how to do this? Sorry for the long mail - I have no idea how to describe the problem shortly and all examples I found always have Gtk.main() called so they are in fact full GTK programs. I want a simple text program with matplotlib window as a "result showing" addition only. Best Regards, Michael Widlok
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180422/76175d36/attachment.html>

Michael,

If you are doing custom embedding I suggest staying away from `pyplot`
altogether, it maintains global state you may not want, see for example
https://matplotlib.org/gallery/user_interfaces/embedding_in_gtk3_sgskip.html#sphx-glr-gallery-user-interfaces-embedding-in-gtk3-sgskip-py
.

For the long answer to your question, see
https://github.com/matplotlib/matplotlib/pull/4779 (which I really need to
finish and get merged). In short, to have both an interactive prompt and a
window you need to have an 'input hook' installed to time-share between the
GUI event loop an the prompt event loop. It sounds like you change a bunch
of things at once (gtk2 -> gtk3 + matplotlib version bumps), did you also
change IPython versions? If so, you will need to use their input hook
mechanism explicitly (`%matplotlib gtk3` should do the trick) as they moved
from readline -> ptprompt so the input hooks the GUI libraries ship no
longer work.

It may also be worth checking the value of `mpl.get_backend()` to make sure
pyplot as not defaulted to another GUI framework (ex Qt5) and you have 2
GUI frameworks in the same process conflicting with each other!

Hope that helps!

Tom

ยทยทยท

On Sun, Apr 22, 2018 at 4:50 PM michalwd1979 <michalwd1979 at o2.pl> wrote:

Dear Members,
Recently I upgraded to from matplotlib 1.4.3 + gtk2 to matplotlib 2.2.2 +
gtk3 and I have a problems with porting my old code.
I'm using matplotlib in Gtk window with typical:
win = Gtk.window()
vbox = Gtk.Box(...)
win.add(vbox)
fig = pylab.figure()
canvas = FigureCanvas(fig)
toolbar = NavigationToolbar(canvas, fig)
axis = fig.add_axes(...)
vbox.pack_start(canvas,...)
vbox.pack_start(toolbar,....)
win.show_all()
while(Gtk.events_pending()): Gtk.main_iteration()

After this code I have a valid window on screen however it does not reacts
on mouse clicks (zooming etc.) until calling Gtk.main_iteration() in loop
or Gtk.main(). I can understand why, however it was working differently
with old matplotlib and gtk2 (window was responsive all time).
The rest of my code is a python "Text User Interface" application
(controlled by commands typed to terminal), the window is only for showing
the results (animated waveform). Now I would like to keep the application
as it is, but also to have responsive matplotlib window, without calling
Gtk.main(). Is it possible? If yes, then how to do this?

Sorry for the long mail - I have no idea how to describe the problem
shortly and all examples I found always have Gtk.main() called so they are
in fact full GTK programs. I want a simple text program with matplotlib
window as a "result showing" addition only.
Best Regards,
Michael Widlok

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180428/052e80cd/attachment.html>

Thank You Tom, It seems that there is in fact quite a lot of changes needed to get this working. First of all I will try to fix that &#34;pylab&#34; issue and then look at the input hooks, but I am not sure if this is still valid for normal python (not IPython - see below). The backend is GTK3Agg - confirmed. I&#39;m not using IPython, just normal python 2.7.14, and this was not changed, so it was only matplotlib and pygtk version change. The old code was creating a &#34;waveform&#34; window when requested (with the code presented before) and for input I was using readline module, the application has set of predefined commands that it executes. Now I am a bit surprised that it was working right from the begging.... (If someone want to see the code then I can post it here) By the way I thought? that having a responsive plot window (in fact only zooming/moving is important) in a command line based application is a common task, but it seems that it is not. Is there any example available for gtk+readline (or any other text input module) usage? Thanks again, Michael Widlok Dnia 28 kwietnia 2018 02:46 Thomas Caswell &lt;tcaswell at gmail.com&gt; napisa?(a): Michael, If you are doing custom embedding I suggest staying away from `pyplot` altogether, it maintains global state you may not want, see for example? matplotlib.org matplotlib.org ?.? For the long answer to your question, see? github.com github.com ?(which I really need to finish and get merged).? In short, to have both an interactive prompt and a window you need to have an &#39;input hook&#39; installed to time-share between the GUI event loop an the prompt event loop.? It sounds like you change a bunch of things at once (gtk2 -&gt; gtk3?+ matplotlib version bumps),? did you also change IPython versions?? If so, you will need to use their input hook mechanism explicitly (`%matplotlib gtk3` should do the trick) as they moved from readline -&gt; ptprompt so the input hooks the GUI libraries ship no longer work.? It may also be worth checking the value of `mpl.get_backend()` to make sure pyplot as not defaulted to another GUI framework (ex Qt5) and you have 2 GUI frameworks in the same process conflicting with each other! Hope that helps! Tom On Sun, Apr 22, 2018 at 4:50 PM michalwd1979 &lt; michalwd1979 at o2.pl &gt; wrote: Dear Members, Recently I upgraded to from matplotlib 1.4.3 + gtk2 to matplotlib 2.2.2 + gtk3 and I have a problems with porting my old code. I&#39;m using matplotlib in Gtk window with typical: win = Gtk.window() vbox = Gtk.Box(...) win.add(vbox) fig = pylab.figure() canvas = FigureCanvas(fig) toolbar = NavigationToolbar(canvas, fig) axis = fig.add_axes(...) vbox.pack_start(canvas,...) vbox.pack_start(toolbar,....) win.show_all() while(Gtk.events_pending()): Gtk.main_iteration() After this code I have a valid window on screen however it does not reacts on mouse clicks (zooming etc.) until calling Gtk.main_iteration() in loop or Gtk.main(). I can understand why, however it was working differently with old matplotlib and gtk2 (window was responsive all time). The rest of my code is a python &#34;Text User Interface&#34; application (controlled by commands typed to terminal), the window is only for showing the results (animated waveform). Now I would like to keep the application as it is, but also to have responsive matplotlib window, without calling Gtk.main(). Is it possible? If yes, then how to do this? Sorry for the long mail - I have no idea how to describe the problem shortly and all examples I found always have Gtk.main() called so they are in fact full GTK programs. I want a simple text program with matplotlib window as a &#34;result showing&#34; addition only. Best Regards, Michael Widlok
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180430/554abeaa/attachment.html>