{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 清除容器\n", "\n", "要从行、列或卡片容器中移除所有元素,可以调用\n", "\n", "```python\n", "container.clear()\n", "```\n", "或者,您可以通过以下方式移除单个元素:\n", "\n", "```python\n", "container.remove(element: Element)\n", "container.remove(index: int)\n", "element.delete()\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "container = ui.row()\n", "\n", "def add_face():\n", " with container:\n", " ui.icon('face')\n", "add_face()\n", "\n", "ui.button('Add', on_click=add_face)\n", "ui.button('Remove', on_click=lambda: container.remove(0) if list(container) else None)\n", "ui.button('Clear', on_click=container.clear)\n", "\n", "# ui.run()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "py311", "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.11.7" } }, "nbformat": 4, "nbformat_minor": 2 }