如何在python jupyter笔记本中创建带有输出的简单Button?

尝试创建带有一些输出的简单按钮,我尝试了以下代码

from IPython.display import display
def clicked():
    print("button has been clicked!")

button_download = widgets.Button(description = 'Test Button')   
button_download.on_click(clicked)
display(button_download)

但是当我单击按钮时,我看不到任何输出。

我发现了另一个可行的示例,但这太复杂了:

from IPython.display import display
button = widgets.Button(description="Click Me!")
output = widgets.Output()

display(button,output)

def on_button_clicked(b):
    with output:
        print("Button clicked.")

button.on_click(on_button_clicked)

我真的需要做output的事情,以便在单击按钮时可以看到print语句的输出吗?

系统为Jupyterlab 1.1.4。

not_mature_enough 回答:如何在python jupyter笔记本中创建带有输出的简单Button?

您只需向clicked函数添加一个参数即可使其起作用:

from IPython.display import display
import ipywidgets as widgets


def clicked(arg):
    print("button has been clicked!")

button_download = widgets.Button(description = 'Test Button')   
button_download.on_click(clicked)
display(button_download)
本文链接:https://www.f2er.com/3157713.html

大家都在问