python web GUI框架-NiceGUI 教程(一)

python web GUI框架-NiceGUI 教程(一)

streamlit可以在一些简单的场景下仍然推荐使用,但是streamlit实在不灵活,受限于它的核心机制,NiceGUI是一个灵活的web框架,可以做web网站也可以打包成独立的exe。

基本元素

Label

显示一些文本。

python 复制代码
from nicegui import ui
ui.label('some label')
ui.run()

Icon

这些元素是基于 Quasar's QIcon 实现的。

python 复制代码
from nicegui import ui
ui.icon('thumb_up', color='primary').classes('text-5xl')
ui.run()

Avatar

avatar 元素是 Quasar's QAvatar 实现的。

python 复制代码
from nicegui import ui
ui.avatar('favorite_border', text_color='grey-11', square=True)
ui.avatar('img:https://nicegui.io/logo_square.png', color='blue-2')
ui.run()

Create a hyperlink.

创建超链接。要跳转到页面中的特定位置,您可以使用ui.link_target("name")放置可链接的锚点,并使用ui.link(target="#name")链接到该锚点。

python 复制代码
from nicegui import ui
ui.link('NiceGUI on GitHub', 'https://github.com/zauberzeug/nicegui')
ui.run()

Button

这个元素是基于Quasar's QBtn 实现的。

颜色参数接受类Quasar颜色、Tailwind颜色或CSS颜色。如果使用Quasar颜色,按钮将根据Quasar主题包括文本的颜色进行样式设置。注意,像"red"这样的颜色既是Quasar的颜色,也是CSS的颜色。在这种情况下,Quasar的颜色将被使用。

python 复制代码
from nicegui import ui
ui.button('Click me!', on_click=lambda: ui.notify(f'You clicked me!'))
ui.run()

Badge

badge 元素基于 Quasar's QBadge 完成。

python 复制代码
from nicegui import ui
with ui.button('Click me!', on_click=lambda: badge.set_text(int(badge.text) + 1)):
    badge = ui.badge('0', color='red').props('floating')
ui.run()

Toggle

这些选项可以指定为值列表,也可以指定为将值映射到标签的字典。在操作选项之后,调用update()来更新UI中的选项。

python 复制代码
from nicegui import ui
toggle1 = ui.toggle([1, 2, 3], value=1)
toggle2 = ui.toggle({1: 'A', 2: 'B', 3: 'C'}).bind_value(toggle1, 'value')
ui.run()

Radio Selection

这些选项可以指定为值列表,也可以指定为将值映射到标签的字典。在操作选项之后,调用update()来更新UI中的选项。

python 复制代码
from nicegui import ui
radio1 = ui.radio([1, 2, 3], value=1).props('inline')
radio2 = ui.radio({1: 'A', 2: 'B', 3: 'C'}).props('inline').bind_value(radio1, 'value')
ui.run()

这些选项可以指定为值列表,也可以指定为将值映射到标签的字典。在操作选项之后,调用update()来更新UI中的选项。

python 复制代码
from nicegui import ui
select1 = ui.select([1, 2, 3], value=1)
select2 = ui.select({1: 'One', 2: 'Two', 3: 'Three'}).bind_value(select1, 'value')
ui.run()

Checkbox

python 复制代码
from nicegui import ui
checkbox = ui.checkbox('check me')
ui.label('Check!').bind_visibility_from(checkbox, 'value')
ui.run()

Switch

python 复制代码
from nicegui import ui
switch = ui.switch('switch me')
ui.label('Switch!').bind_visibility_from(switch, 'value')
ui.run()
相关推荐
蓝天白云下遛狗11 分钟前
goole chrome变更默认搜索引擎为百度
前端·chrome
future141234 分钟前
C#学习日记
开发语言·学习·c#
come1123434 分钟前
Vue 响应式数据传递:ref、reactive 与 Provide/Inject 完全指南
前端·javascript·vue.js
菜包eo34 分钟前
二维码驱动的独立站视频集成方案
网络·python·音视频
Yo_Becky40 分钟前
【PyTorch】PyTorch预训练模型缓存位置迁移,也可拓展应用于其他文件的迁移
人工智能·pytorch·经验分享·笔记·python·程序人生·其他
king_harry1 小时前
Java程序-OceanBase Connector/J 示例
开发语言
yzx9910131 小时前
关于网络协议
网络·人工智能·python·网络协议
fangeqin1 小时前
ubuntu源码安装python3.13遇到Could not build the ssl module!解决方法
linux·python·ubuntu·openssl
musk12121 小时前
electron 打包太大 试试 tauri , tauri 安装打包demo
前端·electron·tauri
傻啦嘿哟2 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#