
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "tutorial/08_widgets/b_checkbox-widget.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_tutorial_08_widgets_b_checkbox-widget.py>`
        to download the full example code. or to run this example in your browser via Binder

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

.. _sphx_glr_tutorial_08_widgets_b_checkbox-widget.py:


.. _checkbox_widget_example:

Checkbox Widget
~~~~~~~~~~~~~~~

Use a checkbox to turn on/off the visibility of meshes in a scene.

See :func:`pyvista.Plotter.add_checkbox_button_widget` for more details.

.. GENERATED FROM PYTHON SOURCE LINES 12-15

.. code-block:: Python


    import pyvista as pv








.. GENERATED FROM PYTHON SOURCE LINES 17-19

Single Checkbox
+++++++++++++++

.. GENERATED FROM PYTHON SOURCE LINES 19-33

.. code-block:: Python


    mesh = pv.Sphere()

    pl = pv.Plotter()
    actor = pl.add_mesh(mesh)


    def toggle_vis(flag) -> None:
        actor.SetVisibility(flag)


    pl.add_checkbox_button_widget(toggle_vis, value=True)
    pl.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/08_widgets/images/sphx_glr_b_checkbox-widget_001.png
        :alt: b checkbox widget
        :srcset: /tutorial/08_widgets/images/sphx_glr_b_checkbox-widget_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/08_widgets/images/sphx_glr_b_checkbox-widget_001.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 34-37

And here is a screen capture of a user interacting with this

.. image:: ../../images/gifs/single-checkbox-widget.gif

.. GENERATED FROM PYTHON SOURCE LINES 39-45

Multiple Checkboxes
+++++++++++++++++++

In this example, we will add many meshes to a scene with unique colors and
create corresponding checkboxes for those meshes of the same color to toggle
their visibility in the scene.

.. GENERATED FROM PYTHON SOURCE LINES 45-65

.. code-block:: Python


    colors = [
        ["ff0000", "28e5da", "0000ff"],
        ["ffff00", "c8bebe", "f79292"],
        ["fffff0", "f18c1d", "23dcaa"],
        ["d785ec", "9d5b13", "e4e0b1"],
        ["894509", "af45f5", "fff000"],
    ]


    class SetVisibilityCallback:
        """Helper callback to keep a reference to the actor being modified."""

        def __init__(self, actor) -> None:
            self.actor = actor

        def __call__(self, state):
            self.actor.SetVisibility(state)









.. GENERATED FROM PYTHON SOURCE LINES 66-92

.. code-block:: Python


    # Widget size
    size = 50

    pl = pv.Plotter()

    Startpos = 12
    for i, lst in enumerate(colors):
        for j, color in enumerate(lst):
            actor = pl.add_mesh(pv.Sphere(center=(i, j, 0)), color=color)
            # Make a separate callback for each widget
            callback = SetVisibilityCallback(actor)
            pl.add_checkbox_button_widget(
                callback,
                value=True,
                position=(5.0, Startpos),
                size=size,
                border_size=1,
                color_on=color,
                color_off="grey",
                background_color="grey",
            )
            Startpos = Startpos + size + (size // 10)

    pl.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/08_widgets/images/sphx_glr_b_checkbox-widget_002.png
        :alt: b checkbox widget
        :srcset: /tutorial/08_widgets/images/sphx_glr_b_checkbox-widget_002.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/08_widgets/images/sphx_glr_b_checkbox-widget_002.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 93-96

And here is a screen capture of a user interacting with this

.. image:: ../../images/gifs/multiple-checkbox-widget.gif

.. GENERATED FROM PYTHON SOURCE LINES 98-105

.. raw:: html

    <center>
      <a target="_blank" href="https://colab.research.google.com/github/pyvista/pyvista-tutorial/blob/gh-pages/notebooks/tutorial/08_widgets/b_checkbox-widget.ipynb">
        <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/ width="150px">
      </a>
    </center>


.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_tutorial_08_widgets_b_checkbox-widget.py:

.. only:: html

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

    .. container:: binder-badge

      .. image:: images/binder_badge_logo.svg
        :target: https://mybinder.org/v2/gh/pyvista/pyvista-tutorial/gh-pages?urlpath=lab/tree/notebooks/tutorial/08_widgets/b_checkbox-widget.ipynb
        :alt: Launch binder
        :width: 150 px

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

      :download:`Download Jupyter notebook: b_checkbox-widget.ipynb <b_checkbox-widget.ipynb>`

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

      :download:`Download Python source code: b_checkbox-widget.py <b_checkbox-widget.py>`

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

      :download:`Download zipped: b_checkbox-widget.zip <b_checkbox-widget.zip>`


.. only:: html

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

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