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()
相关推荐
带娃的IT创业者2 分钟前
Cursor 新增的 Plan 和 Debug 模式介绍
人工智能·python·cursor·ai辅助开发·ai开发工具·ai开发技巧
小北方城市网2 分钟前
第 5 课:服务网格(Istio)实战|大规模微服务的流量与安全治理体系
大数据·开发语言·人工智能·python·安全·微服务·istio
jghhh013 分钟前
自适应信号时频处理方法MATLAB实现(适用于非线性非平稳信号)
开发语言·算法·matlab
AC赳赳老秦3 分钟前
Go语言微服务文档自动化生成:基于DeepSeek的智能解析实践
大数据·开发语言·人工智能·微服务·golang·自动化·deepseek
古城小栈3 分钟前
Rust 之 迭代器
开发语言·rust
r***12383 分钟前
GO 快速升级Go版本
开发语言·redis·golang
祎直向前3 分钟前
linuxshell循环,条件分支语句
前端·chrome
LongtengGensSupreme4 分钟前
开放所有跨域 ----前端和后端
前端·后端·ajax·vue·api·jquery
我算哪枝小绿植4 分钟前
react实现日历拖拽效果
前端·react.js·前端框架
木木木一6 分钟前
Rust学习记录--C5 Rust struct
开发语言·学习·rust