from nicegui import ui
from ex4nicegui.reactive import rxui
from ex4nicegui import to_ref,ref_computed
class page_data:
def __init__(self) -> None:
self.inp_data_1=to_ref('')
self.inp_data_2=to_ref('')
def on_click(self):
print('icon clicked')
def make_calor(self,ref):
@ref_computed
def label_color():
input_text:str=ref.value
start_idx=input_text.find("(")
if start_idx >=0:
end_idx=input_text.find(")",start_idx)
if end_idx > start_idx:
ret=input_text[start_idx+1:end_idx]
return ret
return 'black'
return label_color
def page_run():
data=page_data()
ui.label("方头狮 博客园")
inp_1=rxui.input("输入文字",value=data.inp_data_1)
inp_1.element.props('outlined hint="输入颜色的名称,图标会变色"')
with inp_1.element.add_slot('append'):
ic=rxui.icon('event')
ic.bind_color(data.inp_data_1)
ic.element.on('click',data.on_click)
inp_2=rxui.input("输入文字",value=data.inp_data_2)
inp_2.element.props('outlined hint="输入的文字如果有\\\"(颜色)\\\",图标会变色"')
with inp_2.element.add_slot('append'):
ic=rxui.icon('event')
ic.bind_color(data.make_calor(data.inp_data_2))
ic.element.on('click',data.on_click)
page_run()
ui.run(port=8890)
标签:idx,data,nicegui,inp,第一次,input,ic,ref
From: https://www.cnblogs.com/unm001/p/17547227.html