4
import ipywidgets as widgets

button = widgets.Button(description="Click Me!")
output = widgets.Output()

display(button, output)

def on_button_clicked():
    print("button clicked")

button.on_click(on_button_clicked)

The button shows up.. but when I click on the button, I expect to see the message "button clicked" to show up. But that's not happening.

Is there something I'm missing here?
I am using vscode to run my jupyter notebook.

psj01
  • 3,075
  • 6
  • 32
  • 63

1 Answers1

5

You need to pass the button b in the definition of your on_button_clicked function. See examples in the doc here and code below:

import ipywidgets as widgets

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)

And the output gives:

enter image description here

jylls
  • 4,395
  • 2
  • 10
  • 21