jarvisteach / appJar

Simple Tkinter GUIs in Python
http://appJar.info
Other
615 stars 68 forks source link

how to add matplotlib interactive navigation bar on figures #529

Open pg-doceapower opened 6 years ago

pg-doceapower commented 6 years ago

Context


I'm trying to draw some waveforms in appjar and I could not find how to enable the navigation bar to zoom in/out,... on a plot

matplotlib doc says it is specific to each graphical env and could not see anything in example folder or website help (matplotlib: https://matplotlib.org/users/navigation_toolbar.html)

can you help? Thanks, --Philippe

jarvisteach commented 6 years ago

Hi @pg-doceapower,

I've had a quick dig around, and come up with the following:

from numpy import sin, pi, arange
from appJar import gui 
from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg as addToolbar
import random

def getXY():
    x = arange(0.0, 3.0, 0.01)
    y = sin(random.randint(1,10) * pi * x)
    return x,y 

with gui() as app:
    with app.frame('f1') as frame: pass
    app.addPlot("p1", *getXY())
    addToolbar(app.getPlotWidget("p1"), frame)

There are 3 key parts: 1) Import the toolbar 2) Create a frame to put the toolbar in 3) Create a toolbar, linked to the plot's canvas and frame

It's a bit of a hack - as you have to create an empty frame, which gives a warning & then go get the canvas by calling getPlotWidget()

You can turn off the warnings by calling: app.setLogLevel('ERROR')

jarvisteach commented 6 years ago

This could be included as an additional feature on plots.

jarvisteach commented 6 years ago

This will be in the next release, by setting a flag navbar=True when you add a plot.

jarvisteach commented 6 years ago

This has been updated to resolve: MatplotlibDeprecationWarning: The NavigationToolbar2TkAgg class was deprecated in version 2.2.

Now uses the following for import:

try: from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk as PlotNav
except: from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg as PlotNav
pg-doceapower commented 6 years ago

Hi Jarvis,

as always thanks so much for staying on top of things! There must be something wrong (or different from your setup) since I'm getting this error

2018-08-27 14:14:33,608 appJar:WARNING [Line 16->4022/_removeContainer]: Closing empty container: f1 c:\dev\appJar\appjar.py:6416: MatplotlibDeprecationWarning: The FigureCanvasTk.show function was deprecated in version 2.2. Use FigureCanvasTk.draw instead. canvas.show() 2018-08-27 14:14:33,637 appJar:ERROR [Line 18->1706/exit]: ContextManager failed: cannot use geometry manager pack inside .!canvasdnd.!widgetcontainer which already has slaves managed by grid Traceback (most recent call last): File "test2.py", line 17, in app.addPlot("p1", *getXY()) File "c:\dev\appJar\appjar.py", line 6392, in addPlot canvas, fig = self._addPlotFig(title, row, column, colspan, rowspan, width, height) File "c:\dev\appJar\appjar.py", line 6418, in _addPlotFig canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1) File "C:\Python36_64\lib\tkinter__init__.py", line 2137, in pack_configure

when running your example:

import numpy as np

import matplotlib.pyplot as plt

from appJar import gui

from numpy import sin, pi, arange from appJar import gui from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg import random

def getXY(): x = arange(0.0, 3.0, 0.01) y = sin(random.randint(1,10) pi x) return x,y

with gui() as app: with app.frame('f1') as frame: pass app.addPlot("p1", *getXY()) NavigationToolbar2TkAgg(app.getPlotWidget("p1"), frame)

I'm using appjar 0.93 + python 3.6 64bits. see anything I'm doing wrong? Thanks!

On Mon, Aug 27, 2018 at 9:31 AM Richard Jarvis notifications@github.com wrote:

Closed #529 https://github.com/jarvisteach/appJar/issues/529.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jarvisteach/appJar/issues/529#event-1809946369, or mute the thread https://github.com/notifications/unsubscribe-auth/AGfZynQY3HcXT6UyNdGIUu7OGurlQx2Pks5uU6BKgaJpZM4WHnyq .

jarvisteach commented 6 years ago

I can see what the error message means, and why it might be raised, but I can't reproduce it.

I also think the changes I've just made may even fix it, as I've put the plot & toolbar in their own frame using pack, which is then gridded with everything else...

Can you give me the output from running your code with the -v option:

python3.6 issue529_b.py -v
appJar: 0.93.0 Python: 3.6.2 TCL: 8.5, TK: 8.5 Platform: Darwin pid: 13382
locale: en_GB

Or, if you can't do that, add a menu, and click it...

app.addMenuItem("appJar", "About", app.appJarAbout)
pg-doceapower commented 6 years ago

here is the output:

appJar: 0.93.0 Python: 3.6.0 TCL: 8.6, TK: 8.6 Platform: Windows pid: 6168
locale: en_US