
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/user_interfaces/wxcursor_demo_sgskip.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_user_interfaces_wxcursor_demo_sgskip.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_user_interfaces_wxcursor_demo_sgskip.py:


==================
Add a cursor in WX
==================

Example to draw a cursor and report the data coords in wx.

.. GENERATED FROM PYTHON SOURCE LINES 8-69

.. code-block:: Python


    import wx

    import numpy as np

    from matplotlib.backends.backend_wx import NavigationToolbar2Wx
    from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
    from matplotlib.figure import Figure


    class CanvasFrame(wx.Frame):
        def __init__(self, ):
            super().__init__(None, -1, 'CanvasFrame', size=(550, 350))

            self.figure = Figure()
            self.axes = self.figure.add_subplot()
            t = np.arange(0.0, 3.0, 0.01)
            s = np.sin(2*np.pi*t)

            self.axes.plot(t, s)
            self.axes.set_xlabel('t')
            self.axes.set_ylabel('sin(t)')
            self.figure_canvas = FigureCanvas(self, -1, self.figure)

            # Note that event is a MplEvent
            self.figure_canvas.mpl_connect(
                'motion_notify_event', self.UpdateStatusBar)
            self.figure_canvas.Bind(wx.EVT_ENTER_WINDOW, self.ChangeCursor)

            self.sizer = wx.BoxSizer(wx.VERTICAL)
            self.sizer.Add(self.figure_canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
            self.SetSizer(self.sizer)
            self.Fit()

            self.statusBar = wx.StatusBar(self, -1)
            self.SetStatusBar(self.statusBar)

            self.toolbar = NavigationToolbar2Wx(self.figure_canvas)
            self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
            self.toolbar.Show()

        def ChangeCursor(self, event):
            self.figure_canvas.SetCursor(wx.Cursor(wx.CURSOR_BULLSEYE))

        def UpdateStatusBar(self, event):
            if event.inaxes:
                self.statusBar.SetStatusText(f"x={event.xdata}  y={event.ydata}")


    class App(wx.App):
        def OnInit(self):
            """Create the main window and insert the custom frame."""
            frame = CanvasFrame()
            self.SetTopWindow(frame)
            frame.Show(True)
            return True


    if __name__ == '__main__':
        app = App()
        app.MainLoop()


.. _sphx_glr_download_gallery_user_interfaces_wxcursor_demo_sgskip.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: wxcursor_demo_sgskip.ipynb <wxcursor_demo_sgskip.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: wxcursor_demo_sgskip.py <wxcursor_demo_sgskip.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: wxcursor_demo_sgskip.zip <wxcursor_demo_sgskip.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
