我试图在按钮单击时获取组合框的值并在按钮单击处理程序中处理该值。但是,combobox.v_model 似乎只有在按钮单击处理程序退出后才能正确更新。
这是我所做的(下面的代码):
- 当小部件出现时,在组合框中输入字符串“xxx”
- 之后单击按钮并获取 combobox.v_model 的值
- 预期检索“xxx”,但检索到“”(空字符串)
有没有办法在输入后立即单击按钮来检索组合框内容?
注意:在单击按钮之前按下“Enter”/“TAB”时,一切正常,但如果在组合框中输入后立即按下按钮,则不会。
import ipyvuetify as vue
name = ''
# vuetify combobox and button
combobox = vue.Combobox(label="Enter name", v_model="", items=[], autofocus=True)
btn = vue.Btn(children=['Process name'])
component = vue.Row(children=[
vue.Col(children=[combobox]),
vue.Col(children=[btn])
])
# --- event handler -------------------------
# Some processing of the combobox input needs to happen
# on a button click, but v_model is not updated
def on_button_clicked(widget, event, data):
print(f"btn clicked: {combobox.v_model=}")
name = combobox.v_model
print(f'btn clicked: {name=}')
# do some processing with name here
btn.on_event("click", on_button_clicked)
display(component)