.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/imgui_basic_example.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_gallery_imgui_basic_example.py: ImGui Basic Example ------------------- An example demonstrating a wgpu app with basic imgui usage and events. .. GENERATED FROM PYTHON SOURCE LINES 8-81 .. code-block:: Python # run_example = true import wgpu import sys from imgui_bundle import imgui, imgui_ctx from rendercanvas.auto import RenderCanvas, loop from wgpu.utils.imgui import ImguiRenderer # Create a canvas to render to canvas = RenderCanvas( title="imgui", size=(640, 480), max_fps=60, update_mode="continuous" ) # Create a wgpu device adapter = wgpu.gpu.request_adapter_sync(power_preference="high-performance") device = adapter.request_device_sync() app_state = {"text": "Hello, World\nLorem ipsum, etc.\netc."} imgui_renderer = ImguiRenderer(device, canvas) def update_gui(): if imgui.begin_main_menu_bar(): if imgui.begin_menu("File", True): clicked_quit, _ = imgui.menu_item("Quit", "Cmd+Q", False, True) if clicked_quit: sys.exit(0) imgui.end_menu() imgui.end_main_menu_bar() imgui.set_next_window_size((300, 0), imgui.Cond_.appearing) imgui.set_next_window_pos((0, 20), imgui.Cond_.appearing) imgui.begin("Custom window", None) imgui.text("Example Text") if imgui.button("Hello"): print("World") _, app_state["text"] = imgui.input_text_multiline( "Edit", app_state["text"], imgui.ImVec2(200, 200) ) io = imgui.get_io() imgui.text( f""" Keyboard modifiers: {io.key_ctrl=} {io.key_alt=} {io.key_shift=} {io.key_super=}""" ) if imgui.button("Open popup"): imgui.open_popup("my popup") with imgui_ctx.begin_popup_modal("my popup") as popup: if popup.visible: imgui.text("Hello from popup!") if imgui.button("Close popup"): imgui.close_current_popup() imgui.end() # set the GUI update function that gets called to return the draw data imgui_renderer.set_gui(update_gui) if __name__ == "__main__": canvas.request_draw(imgui_renderer.render) loop.run() .. _sphx_glr_download_gallery_imgui_basic_example.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: imgui_basic_example.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: imgui_basic_example.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: imgui_basic_example.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_ .. only:: html Interactive example =================== This uses Pyodide. If this does not work, your browser may not have sufficient support for wasm/pyodide/wgpu (check your browser dev console). Stdout (print statements) will also appear in the browser console. .. raw:: html