{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `ui.element`\n", "\n", "## 通用元素\n", "\n", "这个类是所有其他UI元素的基类。但是,您可以使用它来创建具有任意HTML标签的元素。\n", "\n", "- `tag`:元素的HTML标签\n", "- `_client`:此元素的客户端(仅用于内部使用)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from nicegui import ui\n", "\n", "with ui.element('div').classes('p-2 bg-blue-100'):\n", " ui.label('inside a colored div')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 移动元素\n", "\n", "此演示展示了如何在容器之间或在容器内移动元素。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "with ui.card() as a:\n", " ui.label('A')\n", " x = ui.label('X')\n", "\n", "with ui.card() as b:\n", " ui.label('B')\n", "\n", "ui.button('Move X to A', on_click=lambda: x.move(a))\n", "ui.button('Move X to B', on_click=lambda: x.move(b))\n", "ui.button('Move X to top', on_click=lambda: x.move(target_index=0))\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 默认属性\n", "\n", "您可以为某个类的所有元素设置默认属性。这样,您就可以避免一遍又一遍地重复相同的属性。\n", "\n", "默认属性仅适用于在设置默认属性后创建的元素。子类继承其父类的默认属性。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "ui.button.default_props('rounded outline')\n", "ui.button('Button A')\n", "ui.button('Button B')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 默认类\n", "\n", "您可以为某个类的所有元素设置默认类。这样,您就可以避免一遍又一遍地重复相同的类。\n", "\n", "默认类仅适用于在设置默认类后创建的元素。子类继承其父类的默认类。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "ui.label.default_classes('bg-blue-100 p-2')\n", "ui.label('Label A')\n", "ui.label('Label B')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 默认样式\n", "\n", "您可以为某个类的所有元素设置默认样式。这样,您就可以避免一遍又一遍地重复相同的样式。\n", "\n", "默认样式仅适用于在设置默认样式后创建的元素。子类继承其父类的默认样式。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "ui.label.default_style('color: tomato')\n", "ui.label('Label A')\n", "ui.label('Label B')\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 }