The button widget

Similarly, we can use a Button widget in our script, as can be seen in the following example

from ipywidgets import widgets 
from IPython.display import display 
 
button = widgets.Button(description="Submit"); 
display(button) 
 
def on_button_clicked(widget): 
    print("Clicked Button:" + widget.description); 
     
button.on_click(on_button_clicked); 

This script does the following:

  •  References the features we want to use from the widgets packages.
  •  Creates our button.
  •  Defines a handler for the click event on a button. The handler receives the Button object that was clicked upon (the widget).
  •  The handler displays information about the button clicked on (you can imagine if we had several buttons in a display, we would want to determine which button was clicked).
  •  Lastly, we assign the defined handler to the Button object we created.
Note the indentation of the coding for the handler; this is the standard Python style that must be followed.

If we run the preceding script in a Notebook, we get a display like the following screenshot:

Note the Submit button at the bottom of the following image. You could change other attributes of the button, such as its position, size, color, and many more. 

If we then click on the Submit button, we get the following display where our message about the button being clicked is displayed:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.6.114