<jupyter_start><jupyter_text>(jupyter:widget-events)= 小部件事件 特殊事件`Button` 不用于表示数据类型。相反，按钮小部件用于处理鼠标单击。：`Button` 的 `on_click` 方法可用于注册当单击按钮时要调用的函数。`on_click` 的文档字符串如下所示。<jupyter_code>import ipywidgets as widgets
print(widgets.Button.on_click.__doc__)<jupyter_output>Register a callback to execute when the button is clicked.

        The callback will be called with one argument, the clicked button
        widget instance.

        Parameters
        ----------
        remove: bool (optional)
            Set to true to remove the callback from the list of callbacks.<jupyter_text>例子由于按钮单击是无状态的，它们使用自定义消息从前端传输到后端。通过使用 `on_click` 方法，下面将显示一个按钮，该按钮在被单击时打印一条消息。要捕获打印（或任何其他类型的输出）并确保它被显示，请确保将其发送到 `Output` 小部件（或将您想要显示的信息放到 `HTML` 小部件中）。<jupyter_code>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, remove=False)
## Traitlet 事件<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10095238095238095, 'fraction_numerical': 0.0, 'mean_word_length': 5.146198830409356, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '597456', 'license': 'Apache-2.0', 'n_tokens_mistral': 465, 'n_tokens_neox': 508, 'n_words': 76, 'path': 'docs/user-guide/04_widget-events.ipynb', 'repo_name': 'xinetzone/ipywidgets-book', 'size': '1050'}