GTKAgg画布和GTKScrolledWindow上的Scroll事件

时间:2018-08-13 11:55:44

标签: python matplotlib gtk pygtk gtk2

我正在尝试使用matplotlib图连接滚动事件

canvas.mpl_connect('scroll_event', on_scroll)

这基本上可以工作,但是如果将图放置在GTKScrolledWindow中,则图对滚动事件做出反应,并且ScrolledWindow也滚动。 如何阻止ScrolledWindow上的事件?有没有办法抓住它?

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()

0 个答案:

没有答案