{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 按钮\n", "\n", "这个元素是基于Quasar的[QBtn](https://quasar.dev/vue-components/button)组件。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`color` 参数接受 Quasar 颜色,Tailwind颜色,或者 CSS 颜色。如果使用 Quasar 颜色,按钮将根据 Quasar 主题进行样式设置,包括文本的颜色。注意,有些颜色如\"红色\"既是 Quasar 颜色也是 CSS 颜色。在这种情况下,将使用 Quasar 颜色。\n", "\n", "- `text`:按钮的标签\n", "- `on_click`:当按钮被按下时调用的回调函数\n", "- `color`:按钮的颜色(可以是 Quasar、Tailwind 或 CSS 颜色,也可以是 `None`,默认为 `'primary'`)\n", "- `icon`:要在按钮上显示的图标的名称(默认为 `None`)" ] }, { "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", "ui.button('Click me!', on_click=lambda: ui.notify('You clicked me!'))\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 图标\n", "\n", "为按钮添加图标。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from nicegui import ui\n", "\n", "with ui.row():\n", " ui.button('demo', icon='history')\n", " ui.button(icon='thumb_up')\n", " with ui.button():\n", " ui.label('sub-elements')\n", " ui.image('https://picsum.photos/id/377/640/360') \\\n", " .classes('rounded-full w-16 h-16 ml-4')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 等待按钮点击\n", "有时候,在继续执行之前等待按钮点击是很方便的。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from nicegui import ui\n", "\n", "@ui.page('/')\n", "async def index():\n", " b = ui.button('Step')\n", " await b.clicked()\n", " ui.label('One')\n", " await b.clicked()\n", " ui.label('Two')\n", " await b.clicked()\n", " ui.label('Three')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 使用上下文管理器禁用按钮\n", "这里展示了一个上下文管理器,它可以用来在异步进程期间禁用按钮。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import httpx\n", "from contextlib import contextmanager\n", "from nicegui import ui\n", "\n", "@contextmanager\n", "def disable(button: ui.button):\n", " button.disable()\n", " try:\n", " yield\n", " finally:\n", " button.enable()\n", "\n", "async def get_slow_response(button: ui.button) -> None:\n", " with disable(button):\n", " async with httpx.AsyncClient() as client:\n", " response = await client.get('https://httpbin.org/delay/1', timeout=5)\n", " ui.notify(f'Response code: {response.status_code}')\n", "\n", "ui.button('Get slow response', on_click=lambda e: get_slow_response(e.sender))\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 自定义切换按钮\n", "就像所有其他元素一样,你可以实现自己的子类,并加入专门的逻辑。比如这个具有内部布尔状态的红/绿切换按钮。\n", "\n", "```python\n", "from nicegui import ui\n", "\n", "class ToggleButton(ui.button):\n", "\n", " def __init__(self, *args, **kwargs) -> None:\n", " super().__init__(*args, **kwargs)\n", " self._state = False\n", " self.on('click', self.toggle)\n", "\n", " def toggle(self) -> None:\n", " \"\"\"Toggle the button state.\"\"\"\n", " self._state = not self._state\n", " self.update()\n", "\n", " def update(self) -> None:\n", " self.props(f'color={\"green\" if self._state else \"red\"}')\n", " super().update()\n", "\n", "ToggleButton('Toggle me')\n", "\n", "ui.run()\n", "```" ] }, { "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 }