{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 查询选择器\n", "\n", "要操作像文档 `body` 这样的元素,您可以使用 `ui.query` 函数。通过查询结果,您可以像操作其他 UI 元素一样添加类、样式和属性。例如,这可以用于更改页面背景颜色(例如 `ui.query('body').classes('bg-green')`)。\n", "\n", "`selector`:CSS 选择器(例如 `\"body\"`、`\"#my-id\"`、`\".my-class\"`、`\"div > p\"`)" ] }, { "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", "def set_background(color: str) -> None:\n", " ui.query('body').style(f'background-color: {color}')\n", "\n", "ui.button('Blue', on_click=lambda: set_background('#ddeeff'))\n", "ui.button('Orange', on_click=lambda: set_background('#ffeedd'))\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 设置背景渐变\n", "\n", "设置背景渐变、图像或类似效果非常简单。有关使用 CSS 设置背景的更多信息,请参见 [`w3schools.com`](https://www.w3schools.com/cssref/pr_background-image.php)。" ] }, { "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", "ui.query('body').classes('bg-gradient-to-t from-blue-400 to-blue-100')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 修改默认页面内边距\n", "\n", "默认情况下,NiceGUI 在页面内容周围提供了一个内置的内边距。您可以使用类选择器 `.nicegui-content` 来修改它。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from nicegui import ui\n", "\n", "ui.query('.nicegui-content').classes('p-0')\n", "with ui.column().classes('h-screen w-full bg-gray-400 justify-between'):\n", " ui.label('top left')\n", " ui.label('bottom right').classes('self-end')\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 }