Sophie

Sophie

distrib > Fedora > 18 > i386 > by-pkgid > bf21b4394f4d7fa09e3626145d3315e0 > files > 210

python-matplotlib-doc-1.2.0-14.fc18.i686.rpm

.. _event_handling-test_mouseclicks:

event_handling example code: test_mouseclicks.py
================================================

[`source code <test_mouseclicks.py>`_]

::

    #!/usr/bin/env python
    from __future__ import print_function
    
    import matplotlib
    #matplotlib.use("WxAgg")
    #matplotlib.use("TkAgg")
    #matplotlib.use("GTKAgg")
    #matplotlib.use("QtAgg")
    #matplotlib.use("Qt4Agg")
    #matplotlib.use("CocoaAgg")
    #matplotlib.use("MacOSX")
    import matplotlib.pyplot as plt
    
    #print("***** TESTING WITH BACKEND: %s"%matplotlib.get_backend() + " *****")
    
    
    def OnClick(event):
        if event.dblclick:
            print("DBLCLICK", event)
        else:
            print("DOWN    ", event)
    
    
    def OnRelease(event):
        print("UP      ", event)
    
    
    fig = plt.gcf()
    cid_up = fig.canvas.mpl_connect('button_press_event', OnClick)
    cid_down = fig.canvas.mpl_connect('button_release_event', OnRelease)
    
    plt.gca().text(0.5, 0.5, "Click on the canvas to test mouse events.",
                   ha="center", va="center")
    
    plt.show()
    

Keywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)