.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/events/image_click.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_events_image_click.py: Image click event ================= Example showing how to use a click event on an image. .. GENERATED FROM PYTHON SOURCE LINES 7-44 .. image-sg:: /_gallery/events/images/sphx_glr_image_click_001.webp :alt: image click :srcset: /_gallery/events/images/sphx_glr_image_click_001.webp :class: sphx-glr-single-img .. code-block:: Python # test_example = false import fastplotlib as fpl import pygfx import imageio.v3 as iio data = iio.imread("imageio:camera.png") # Create a figure figure = fpl.Figure(size=(700, 560)) # create image graphic image_graphic = figure[0, 0].add_image(data=data) # show the plot figure.show() # adding a click event, we can also use decorators to add event handlers @image_graphic.add_event_handler("click") def click_event(ev: pygfx.PointerEvent): # get the click location in screen coordinates xy = (ev.x, ev.y) # map the screen coordinates to world coordinates xy = figure[0, 0].map_screen_to_world(xy)[:-1] # print the click location print(xy) # NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively # please see our docs for using fastplotlib interactively in ipython and jupyter if __name__ == "__main__": print(__doc__) fpl.loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.527 seconds) .. _sphx_glr_download__gallery_events_image_click.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: image_click.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: image_click.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: image_click.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_