量化交易之nicegui篇 - row/grid/控件交互回调函数

本文详细介绍了如何在NiceGUI库中创建各种UI元素(如按钮、对话框、卡片、网格、标签等),以及如何实现控件间的交互,包括事件绑定、状态切换和属性绑定。
摘要由CSDN通过智能技术生成
""" pop a controller when click button.
with ui.dialog() as dialog, ui.card():
    ui.button('close', on_click=dialog.close)
    ui.label('hello world!')

test_button = ui.button('跳转', on_click=dialog.open)
ui.run()
"""

""" open new window with url when click button.
url = 'https://github.com/zauberzeug/nicegui/'
ui.button('Open GitHub', on_click=lambda: ui.open(url, new_tab=True))
ui.run()
"""
""" row
with ui.row():
    ui.label('label 1')
    ui.label('label 2')
    ui.label('label 3')

ui.run()
"""
""" 网格 
with ui.grid(columns=2):
    ui.label('Name: ')
    ui.label('Tom: ')

    ui.label('Age: ')
    ui.label('42')

    ui.label('Height: ')
    ui.label('1.80m')

ui.run()
"""
""" 卡片的突起效果, 并在卡片中添加两个label;
with ui.card().tight():
    with ui.card_section():
        ui.label('ui.card_section')
        ui.label('ui.card_section')
"""
""" 竖线, 禁止拖拽 
with ui.splitter() as splitter:
    with splitter.before:
        ui.label('This is some content on the left hand side.').classes('mr-2')
    with splitter.after:
        ui.label('This is some content on the right hand side.').classes('ml-2')

splitter.enabled = False
ui.run()
"""
""" tab items
with ui.tabs().classes('w-full') as tabs:
    one = ui.tab('One')
    two = ui.tab('Two')

with ui.tab_panels(tabs, value=two).classes('w-full'):
    with ui.tab_panel(one):
        ui.label('First tab')
    with ui.tab_panel(two):
        ui.label('Second tab')

ui.run()
"""
""" 控件绑定方法, 并在callback函数中识别对应控件;
from nicegui.events import ValueChangeEventArguments

def show(event: ValueChangeEventArguments):
    name = type(event.sender).__name__
    print('name: ' + str(name) + ' event.value: ' + str(event.value))

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()
"""
""" 控件变化, 修改属性里面的值;
class Demo:
    def __init__(self):
        self.number = 1

demo = Demo()
v = ui.checkbox('visible', value=True)
with ui.column().bind_visibility_from(v, 'value'):
    ui.slider(min=1, max=3).bind_value(demo, 'number')
    ui.toggle({1: 'A', 2: 'B', 3: 'C'}).bind_value(demo, 'number')
    ui.number().bind_value(demo, 'number')

ui.run()
"""
""" 点击button之后, 修改label的值 
from nicegui.events import ValueChangeEventArguments

switch = ui.switch('switch me')
ret_label = ui.label('Switch!').bind_visibility_from(switch, 'value')

def button_click(event: ValueChangeEventArguments):
    # print('event: ' + str(event), end=' ')
    print('switch value: ' + str(switch.value), end=' ')
    ret_label.text += ' text '
    print(' ret_label text: ' + str(ret_label.text))

ui.button('test button.', on_click=button_click)

ui.run()
"""
""" 需要一个不能拖拽, 但是程序可以控制进度的滑动条
slider = ui.slider(min=0, max=1, step=0.01, value=0.3)
slider.visible = False
ui.linear_progress().bind_value_from(slider, 'value')

# @ui.refreshable
def button_click():
    slider.value = round(slider.value + 0.1, 1)
    slider.update()

ui.button('add value', on_click=button_click)

ui.run()
"""
""" 两个button之间互相控制对方的可用状态; (两种方式)
def callback_func(event):
    if event == 'button1_Yes':  # click button1
        pass
    elif event == 'button2_No':  # click button2
        pass

    button1.enabled = not button1.enabled
    button2.enabled = not button2.enabled

button1 = ui.button('Yes', on_click=lambda: callback_func('button1_Yes'))
button2 = ui.button('No', on_click=lambda: callback_func('button2_No'))
button2.set_enabled(value=False)

ui.run()

from nicegui.events import ValueChangeEventArguments

def callback_func(event: ValueChangeEventArguments):
    button1.enabled = not button1.enabled
    button2.enabled = not button2.enabled

button1 = ui.button('Yes', on_click=callback_func)
button2 = ui.button('No', on_click=callback_func)
button2.enabled = False

ui.run()
"""
""" 测试封装的控件
def create_label(text: str):
    with ui.label(text=f'{text} {text}') as new_label:
        pass

    return new_label

some_label = create_label(text='hello')

def button_click():
    some_label.text = 'world world'

ui.button('click', on_click=button_click)
ui.run()
"""

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值