一、目标
学习Tkinter制作窗体软件的基础,鼠标相关的事件。
Button-1> 鼠标左键 <Button-2> 鼠标中间键(滚轮) <Button-3> 鼠标右键 <Double-Button-1> 双击鼠标左键 <Double-Button-3> 双击鼠标右键 <Triple-Button-1> 三击鼠标左键 <Triple-Button-3> 三击鼠标右键
鼠标移动事件 <B1-Motion> 鼠标左键滑动 <B2-Motion> 鼠标滚轮移动 <B3-Motion> 鼠标右键滑动
二、试验平台
windows7 , python3.7
三、鼠标点击事件示例
import tkinter
from tkinter import ttk
def xFunc1(event):
print(f"鼠标左键点击了一次坐标是:x={event.x}y={event.y}")
win = tkinter.Tk()
win.title("Kahn Software v1") # #窗口标题
win.geometry("600x500+200+20") # #窗口位置500后面是字母x
'''
鼠标点击事件
<Button-1> 鼠标左键
<Button-2> 鼠标中间键(滚轮)
<Button-3> 鼠标右键
<Double-Button-1> 双击鼠标左键
<Double-Button-3> 双击鼠标右键
<Triple-Button-1> 三击鼠标左键
<Triple-Button-3> 三击鼠标右键
'''
button1 =tkinter.Button(win, text="leftmouse button")
# button1 =tkinter.Label(win, text="leftmouse button") # #任何的小空间都可以绑定鼠标事件
button1.bind("<Button-1>", xFunc1) # #给按钮控件绑定左键单击事件
button1.pack()
win.mainloop() # #窗口持久化
四、鼠标点击后移动事件
import tkinter
from tkinter import ttk
def xFunc1(event):
print(f"鼠标左键滑动坐标是:x={event.x},y={event.y}")
win = tkinter.Tk()
win.title("Kahn Software v1") # #窗口标题
win.geometry("600x500+200+20") # #窗口位置500后面是字母x
'''
鼠标移动事件
<B1-Motion> 鼠标左键滑动
<B2-Motion> 鼠标滚轮移动
<B3-Motion> 鼠标右键滑动
'''
xLabel = tkinter.Label(win, text="KAHN Hello world")
xLabel.pack()
xLabel.bind("<B1-Motion>", xFunc1)
win.mainloop() # #窗口持久化
六、释放鼠标按键触发
经测试,必须先点到有效区域,同时在有效区域上释放才行
import tkinter
from tkinter import ttk
def xFunc1(event):
print(f"鼠标左键释放坐标是:x={event.x},y={event.y}")
win = tkinter.Tk()
win.title("Kahn Software v1") # #窗口标题
win.geometry("600x500+200+20") # #窗口位置500后面是字母x
'''
鼠标释放事件
<ButtonRelease-1> 鼠标释放左键触发(经测试,必须先点到有效区域,同时在有效区域上释放才行)
<ButtonRelease-2> 释放鼠标滚轮
<ButtonRelease-3> 释放鼠标右键
<Enter> 鼠标进入触发事件,仅一次有效。下次光标需移出有效区域再次进入时才再次触发
<Leave> 鼠标离开触发事件,离开那一刹那触发
'''
xLabel = tkinter.Label(win, text="KAHN Hello world")
xLabel.pack()
xLabel.bind("<ButtonRelease-1>", xFunc1)
win.mainloop() # #窗口持久化
kahn 2019年5月1日10:44:18