以下是一个示例代码,用于捕获鼠标事件:左键按下 + 鼠标离开。
import tkinter as tk
def on_mouse_down(event):
if event.num == 1: # 左键按下
print("Left mouse button down")
def on_mouse_leave(event):
print("Mouse left the widget")
root = tk.Tk()
# 创建一个 Frame 作为事件容器
frame = tk.Frame(root, width=300, height=200)
frame.bind("
在上述示例代码中,我们使用了 tkinter 库来创建一个 GUI 应用程序。首先,我们创建了一个 Frame 对象作为事件容器,并指定了宽度和高度。然后,我们通过 bind
方法将 on_mouse_down
函数绑定到 事件上,该事件表示鼠标按下。我们还将
on_mouse_leave
函数绑定到
事件上,该事件表示鼠标离开。最后,我们将 Frame 对象添加到主窗口中,并通过 mainloop
方法启动应用程序。
当鼠标左键按下时,on_mouse_down
函数将被调用,并输出 "Left mouse button down"。当鼠标离开事件容器时,on_mouse_leave
函数将被调用,并输出 "Mouse left the widget"。
您可以根据实际需求修改这些函数,以执行您想要的操作。