.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_ui/viz_buttons.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_07_ui_viz_buttons.py: =============== Buttons & Text =============== This example shows how to use the UI API. We will demonstrate how to create panel having buttons with callbacks. First, some imports. .. GENERATED FROM PYTHON SOURCE LINES 12-16 .. code-block:: Python from fury import ui, window from fury.data import fetch_viz_icons, read_viz_icons .. GENERATED FROM PYTHON SOURCE LINES 17-18 First we need to fetch some icons that are included in FURY. .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. code-block:: Python fetch_viz_icons() .. GENERATED FROM PYTHON SOURCE LINES 22-24 Let's create some buttons and text and put them in a panel. First we'll make the panel. .. GENERATED FROM PYTHON SOURCE LINES 24-28 .. code-block:: Python panel = ui.Panel2D(size=(300, 150), color=(1, 1, 1), align="right") panel.center = (500, 400) .. GENERATED FROM PYTHON SOURCE LINES 29-31 Then we'll make two text labels and place them on the panel. Note that we specify the position with integer numbers of pixels. .. GENERATED FROM PYTHON SOURCE LINES 31-37 .. code-block:: Python text = ui.TextBlock2D(text="Click me") text2 = ui.TextBlock2D(text="Me too") panel.add_element(text, (50, 100)) panel.add_element(text2, (180, 100)) .. GENERATED FROM PYTHON SOURCE LINES 38-42 Then we'll create two buttons and add them to the panel. Note that here we specify the positions with floats. In this case, these are percentages of the panel size. .. GENERATED FROM PYTHON SOURCE LINES 42-60 .. code-block:: Python button_example = ui.Button2D( icon_fnames=[("square", read_viz_icons(fname="stop2.png"))] ) icon_files = [] icon_files.append(("down", read_viz_icons(fname="circle-down.png"))) icon_files.append(("left", read_viz_icons(fname="circle-left.png"))) icon_files.append(("up", read_viz_icons(fname="circle-up.png"))) icon_files.append(("right", read_viz_icons(fname="circle-right.png"))) second_button_example = ui.Button2D(icon_fnames=icon_files) panel.add_element(button_example, (0.25, 0.33)) panel.add_element(second_button_example, (0.66, 0.33)) .. GENERATED FROM PYTHON SOURCE LINES 61-62 We can add a callback to each button to perform some action. .. GENERATED FROM PYTHON SOURCE LINES 62-77 .. code-block:: Python def change_text_callback(i_ren, _obj, _button): text.message = "Clicked!" i_ren.force_render() def change_icon_callback(i_ren, _obj, _button): _button.next_icon() i_ren.force_render() button_example.on_left_mouse_button_clicked = change_text_callback second_button_example.on_left_mouse_button_pressed = change_icon_callback .. GENERATED FROM PYTHON SOURCE LINES 78-80 Now that all the elements have been initialised, we add them to the show manager. .. GENERATED FROM PYTHON SOURCE LINES 80-92 .. code-block:: Python current_size = (800, 800) show_manager = window.ShowManager(size=current_size, title="FURY Button Example") show_manager.scene.add(panel) interactive = False if interactive: show_manager.start() window.record(show_manager.scene, size=current_size, out_path="viz_button.png") .. _sphx_glr_download_auto_examples_07_ui_viz_buttons.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_buttons.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_buttons.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_