3

I am trying to connect the scroll event in my matplotlib plots using

canvas.mpl_connect('scroll_event', on_scroll)

This works basically, but if the plots are placed in a GTKScrolledWindow, the plot reacts to the scroll event and the ScrolledWindow scrolls too. How can I block the event on the ScrolledWindow? Is there a way to catch it?

import gtk
from matplotlib.figure import Figure
import numpy as np
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as 
FigureCanvas

def main():

    win = gtk.Window()
    win.connect("destroy", lambda x: gtk.main_quit())
    win.set_default_size(400, 400)
    win.set_title("Embedding in GTK")
    scrolled_window = gtk.ScrolledWindow()
    scrolled_window.set_policy(gtk.POLICY_ALWAYS, gtk.POLICY_ALWAYS)
    win.add(scrolled_window)
    vbox = gtk.VBox()
    vbox.set_size_request(600,600)
    scrolled_window.add_with_viewport(vbox)

    fig = Figure(figsize=(5, 4), dpi=100)
    ax = fig.add_subplot(111)
    t = np.arange(0.0, 3.0, 0.01)
    s = np.sin(2*np.pi*t)

    ax.plot(t, s)

    canvas = FigureCanvas(fig)  # a gtk.DrawingArea
    vbox.pack_start(canvas)

    def on_scroll(event):
        print('you scrolled')

    canvas.mpl_connect('scroll_event', on_scroll)

    win.show_all()
    gtk.main()

if __name__ == '__main__':
    main()
pxe
  • 155
  • 7

0 Answers0