{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `ui.label`\n", "\n", "## 标签\n", "\n", "显示一些文本。\n", "\n", "- `text`:标签的内容" ] }, { "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.label('some label')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 根据内容改变外观\n", "\n", "您可以重写 `_handle_text_change` 方法,根据标签的内容来更新其其他属性。这种技术也适用于绑定,如下所示的示例中所示。\n", "\n", "```python\n", "from nicegui import ui\n", "\n", "class status_label(ui.label):\n", " def _handle_text_change(self, text: str) -> None:\n", " super()._handle_text_change(text)\n", " if text == 'ok':\n", " self.classes(replace='text-positive')\n", " else:\n", " self.classes(replace='text-negative')\n", "\n", "model = {'status': 'error'}\n", "status_label().bind_text_from(model, 'status')\n", "ui.switch(on_change=lambda e: model.update(status='ok' if e.value else 'error'))\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 }