python 破折号dcc输入,禁用属性是做相反的我想要它太多

bmvo0sr5  于 2023-02-11  发布在  Python
关注(0)|答案(1)|浏览(108)
layout = html.Div([
    html.H1("Registration"),
    html.H4("Username: "),
    dcc.Input(id="username"),
    
    html.Div([
        html.H3(option[0]),
        html.Button("Yes", id="stock_y", n_clicks=0),
        html.Button("No", id="stock_n", n_clicks=0),
        html.Br(),
        dcc.Input(id="stock", placeholder="Please enter stock here", disabled="True")
]),

    ])

@callback(
    Output("stock", component_property="disabled"),
    Input("stock_y", "n_clicks")
)
def enable(clicks):
    if clicks > 0:
        return "False"

我希望文本框最初被禁用,如果用户单击"是"按钮,则将启用文本框。但是,当我打开页面时,文本框被启用,当我单击"是"按钮时,它将禁用文本框。为什么会出现这种情况,如何解决?
示例:https://imgur.com/a/BqLFNID
尝试更改false和true语句,并预期框最初被禁用,并在通过将disabled属性从"True"交换为"False"点击"是"后变为启用

wnavrhmk

wnavrhmk1#

值需要是布尔值,而不是字符串。因此,您应该具有:

dcc.Input(id="stock", placeholder="Please enter stock here", disabled=True)

在回调中:

def enable(clicks):
    if clicks > 0:
        return False
    return True  # good to set a default, otherwise it will be set to None

相关问题