{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Checkbox Widget {#checkbox_widget_example}\n\nUse a checkbox to turn on/off the visibility of meshes in a scene.\n\nSee `pyvista.Plotter.add_checkbox_button_widget`{.interpreted-text\nrole=\"func\"} for more details.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pyvista as pv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Single Checkbox\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = pv.Sphere()\n\np = pv.Plotter()\nactor = p.add_mesh(mesh)\n\n\ndef toggle_vis(flag):\n actor.SetVisibility(flag)\n\n\np.add_checkbox_button_widget(toggle_vis, value=True)\np.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Multiple Checkboxes\n\nIn this example, we will add many meshes to a scene with unique colors\nand create corresponding checkboxes for those meshes of the same color\nto toggle their visibility in the scene.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "colors = [\n [\"ff0000\", \"28e5da\", \"0000ff\"],\n [\"ffff00\", \"c8bebe\", \"f79292\"],\n [\"fffff0\", \"f18c1d\", \"23dcaa\"],\n [\"d785ec\", \"9d5b13\", \"e4e0b1\"],\n [\"894509\", \"af45f5\", \"fff000\"],\n]\n\n\nclass SetVisibilityCallback:\n \"\"\"Helper callback to keep a reference to the actor being modified.\"\"\"\n\n def __init__(self, actor):\n self.actor = actor\n\n def __call__(self, state):\n self.actor.SetVisibility(state)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Widget size\nsize = 50\n\np = pv.Plotter()\n\nStartpos = 12\nfor i, lst in enumerate(colors):\n for j, color in enumerate(lst):\n actor = p.add_mesh(pv.Sphere(center=(i, j, 0)), color=color)\n # Make a separate callback for each widget\n callback = SetVisibilityCallback(actor)\n p.add_checkbox_button_widget(\n callback,\n value=True,\n position=(5.0, Startpos),\n size=size,\n border_size=1,\n color_on=color,\n color_off='grey',\n background_color='grey',\n )\n Startpos = Startpos + size + (size // 10)\n\np.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }