python 梯度:当值低于第一个滑块时,更改第二个滑块的位置

bq3bfh9z  于 2023-06-20  发布在  Python
关注(0)|答案(1)|浏览(88)

我在Gradio中创建了两个滑块:

number1 = gr.Slider(0, 20, step=1, label='number1', default=0, interactive=True)
 number2 = gr.Slider(0, 20, step=1, label='number2', default=0, interactive=True)

 gr.Interface(add_function, [number1, number2], "number", live=True).launch(debug=True)

我想动态地改变第二个滑块的位置。当第一个滑块的值大于第二个滑块时,第二个滑块应与第一个滑块获得相同的位置,例如。first slider = 4 second slider = 1然后second的滑块位置也应该变为4。
可以使用gr.Interface还是只使用Gradio Box

xzabzqsa

xzabzqsa1#

好的,我用Gradio Box找到了解决方案:

def compare(number1, number2):
    if number1 > number2:
        number2 = number1
    return number2

def add_function(number1, number2):
    return number2 + number1

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            slider1 = gr.Slider(0, 100, default=50, label="number1", interactive=True)
            slider2 = gr.Slider(0, 100, default=50, label="number2", interactive=True)
        with gr.Column():
            predict = gr.Button()
            output = gr.Number(label="output")

    slider1.change(compare, [slider1, slider2], slider2)
    click_event = predict.click(add_function, [slider1, slider2] , output)

demo.queue().launch()

没有那么快,但解决我的问题:)

相关问题