这是我用过最爽的gui模块了, 因为我本身会点html和js, 当然了这个模块还有其他好处
示例
from nicegui import ui
from nicegui.events import ValueChangeEventArguments
def show(event: ValueChangeEventArguments):
name = type(event.sender).__name__
ui.notify(f'{name}: {event.value}')
ui.button('Button', on_click=lambda: ui.notify('Click'))
with ui.row():
ui.checkbox('Checkbox', on_change=show)
ui.switch('Switch', on_change=show)
ui.radio(['A', 'B', 'C'], value='A', on_change=show).props('inline')
with ui.row():
ui.input('Text input', on_change=show)
ui.select(['One', 'Two'], value='One', on_change=show)
ui.run(reload=False, native=True)
结束语
之前用过其他gui模块, 比如pyqt, simplegui 等等, 但是这个目前来说非常简单而且功能强大, github刚出就已经有4k star了
打包
这个是虚拟环境打包, 创建一个打包.py文件, 并且要与项目文件同级, 使用的是pyinstaller, 需要注意的是, 打包时, ui那边需要设置ui.run(reload=False, native=True)
import os
import subprocess
from pathlib import Path
import sys
import nicegui
script_path = Path(__file__) / "demo.py" # 要打包的app.py位置
venv_path = sys.prefix # 当前 Python 解释器的虚拟环境路径
pyinstaller_path = Path(venv_path , "Scripts", "pyinstaller.exe") # 虚拟环境中的 PyInstaller 可执行文件路径
cmd = [
pyinstaller_path,
"--name", "myapp",
"--onedir",
"--windowed",
"--clean",
"--add-data", f'{Path(nicegui.__file__).parent}{os.pathsep}nicegui',
str(script_path)
]
subprocess.call(cmd)