
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/user_interfaces/mathtext_wx_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_mathtext_wx_sgskip.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_user_interfaces_mathtext_wx_sgskip.py:


======================
Display mathtext in WX
======================

Demonstrates how to convert (math)text to a wx.Bitmap for display in various
controls on wxPython.

.. GENERATED FROM PYTHON SOURCE LINES 9-134

.. code-block:: Python


    from io import BytesIO

    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

    IS_WIN = 'wxMSW' in wx.PlatformInfo


    def mathtext_to_wxbitmap(s):
        # We draw the text at position (0, 0) but then rely on
        # ``facecolor="none"`` and ``bbox_inches="tight", pad_inches=0`` to get a
        # transparent mask that is then loaded into a wx.Bitmap.
        fig = Figure(facecolor="none")
        text_color = (
            np.array(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)) / 255)
        fig.text(0, 0, s, fontsize=10, color=text_color)
        buf = BytesIO()
        fig.savefig(buf, format="png", dpi=150, bbox_inches="tight", pad_inches=0)
        s = buf.getvalue()
        return wx.Bitmap.NewFromPNGData(s, len(s))


    functions = [
        (r'$\sin(2 \pi x)$', lambda x: np.sin(2*np.pi*x)),
        (r'$\frac{4}{3}\pi x^3$', lambda x: (4/3)*np.pi*x**3),
        (r'$\cos(2 \pi x)$', lambda x: np.cos(2*np.pi*x)),
        (r'$\log(x)$', lambda x: np.log(x))
    ]


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

            self.figure = Figure()
            self.axes = self.figure.add_subplot()

            self.canvas = FigureCanvas(self, -1, self.figure)

            self.change_plot(0)

            self.sizer = wx.BoxSizer(wx.VERTICAL)
            self.add_buttonbar()
            self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
            self.add_toolbar()  # comment this out for no toolbar

            menuBar = wx.MenuBar()

            # File Menu
            menu = wx.Menu()
            m_exit = menu.Append(
                wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")
            menuBar.Append(menu, "&File")
            self.Bind(wx.EVT_MENU, self.OnClose, m_exit)

            if IS_WIN:
                # Equation Menu
                menu = wx.Menu()
                for i, (mt, func) in enumerate(functions):
                    bm = mathtext_to_wxbitmap(mt)
                    item = wx.MenuItem(menu, 1000 + i, " ")
                    item.SetBitmap(bm)
                    menu.Append(item)
                    self.Bind(wx.EVT_MENU, self.OnChangePlot, item)
                menuBar.Append(menu, "&Functions")

            self.SetMenuBar(menuBar)

            self.SetSizer(self.sizer)
            self.Fit()

        def add_buttonbar(self):
            self.button_bar = wx.Panel(self)
            self.button_bar_sizer = wx.BoxSizer(wx.HORIZONTAL)
            self.sizer.Add(self.button_bar, 0, wx.LEFT | wx.TOP | wx.GROW)

            for i, (mt, func) in enumerate(functions):
                bm = mathtext_to_wxbitmap(mt)
                button = wx.BitmapButton(self.button_bar, 1000 + i, bm)
                self.button_bar_sizer.Add(button, 1, wx.GROW)
                self.Bind(wx.EVT_BUTTON, self.OnChangePlot, button)

            self.button_bar.SetSizer(self.button_bar_sizer)

        def add_toolbar(self):
            """Copied verbatim from embedding_wx2.py"""
            self.toolbar = NavigationToolbar2Wx(self.canvas)
            self.toolbar.Realize()
            # By adding toolbar in sizer, we are able to put it at the bottom
            # of the frame - so appearance is closer to GTK version.
            self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
            # update the axes menu on the toolbar
            self.toolbar.update()

        def OnChangePlot(self, event):
            self.change_plot(event.GetId() - 1000)

        def change_plot(self, plot_number):
            t = np.arange(1.0, 3.0, 0.01)
            s = functions[plot_number][1](t)
            self.axes.clear()
            self.axes.plot(t, s)
            self.canvas.draw()

        def OnClose(self, event):
            self.Destroy()


    class MyApp(wx.App):
        def OnInit(self):
            frame = CanvasFrame(None, "wxPython mathtext demo app")
            self.SetTopWindow(frame)
            frame.Show(True)
            return True


    if __name__ == "__main__":
        app = MyApp()
        app.MainLoop()


.. _sphx_glr_download_gallery_user_interfaces_mathtext_wx_sgskip.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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