

.. _sphx_glr_gallery_user_interfaces_embedding_in_gtk2_sgskip.py:


=================
Embedding In GTK2
=================

show how to add a matplotlib FigureCanvasGTK or FigureCanvasGTKAgg widget and
a toolbar to a gtk.Window



.. code-block:: python

    import gtk

    from matplotlib.figure import Figure
    from numpy import arange, sin, pi

    # uncomment to select /GTK/GTKAgg/GTKCairo
    #from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
    from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
    #from matplotlib.backends.backend_gtkcairo import FigureCanvasGTKCairo as FigureCanvas

    # or NavigationToolbar for classic
    #from matplotlib.backends.backend_gtk import NavigationToolbar2GTK as NavigationToolbar
    from matplotlib.backends.backend_gtkagg import NavigationToolbar2GTKAgg as NavigationToolbar

    # implement the default mpl key bindings
    from matplotlib.backend_bases import key_press_handler

    win = gtk.Window()
    win.connect("destroy", lambda x: gtk.main_quit())
    win.set_default_size(400, 300)
    win.set_title("Embedding in GTK")

    vbox = gtk.VBox()
    win.add(vbox)

    fig = Figure(figsize=(5, 4), dpi=100)
    ax = fig.add_subplot(111)
    t = arange(0.0, 3.0, 0.01)
    s = sin(2*pi*t)

    ax.plot(t, s)


    canvas = FigureCanvas(fig)  # a gtk.DrawingArea
    vbox.pack_start(canvas)
    toolbar = NavigationToolbar(canvas, win)
    vbox.pack_start(toolbar, False, False)


    def on_key_event(event):
        print('you pressed %s' % event.key)
        key_press_handler(event, canvas, toolbar)

    canvas.mpl_connect('key_press_event', on_key_event)

    win.show_all()
    gtk.main()

**Total running time of the script:** ( 0 minutes  0.000 seconds)



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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

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

    `Generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
