python 按钮事件
时间: 2023-10-12 19:12:44 浏览: 108
在 Python 中,可以使用 GUI 库如 tkinter、PyQt、wxPython 等来创建按钮并添加事件。以下是使用 tkinter 创建按钮并添加事件的示例代码:
```python
import tkinter as tk
def button_click():
print("Button clicked")
root = tk.Tk()
button = tk.Button(root, text="Click me", command=button_click)
button.pack()
root.mainloop()
```
在上面的示例代码中,我们使用 tkinter 创建了一个名为 root 的主窗口,并在其上创建了一个按钮。我们将按钮的文本设置为 "Click me",并将按钮的 command 参数设置为 button_click 函数。当用户单击按钮时,程序将调用 button_click 函数并输出一条消息 "Button clicked"。
可以根据实际需求来修改 button_click 函数的实现,实现各种不同的按钮事件。
阅读全文
相关推荐












