除了前面提到的Button中绑定command的事件之外,还有一种使用bind进行绑定事件的方式

介绍几个常用的鼠标事件:

< Button-1> 单击鼠标左键
< Button-3> 单击鼠标右键
< Button-4> 鼠标滚轮向上滚动
< Button-5> 鼠标滚轮上下滚动
< Motion> 鼠标移动-
< ButtonRelease-1> 松开鼠标左键
< DoubleButton-1> 双击鼠标左键

2. 上代码

from tkinter import*
def showGrilFriend():
    lbl.config(text="我的名字叫女朋友")
root=Tk()
root.title("Print GrilFriend")
root.geometry("300x200")
btn=Button(root,text="打印",command=showGrilFriend)
btn.pack(anchor=S)
# 创建一个显示用的Label
lbl=Label(root,bg="yellow",fg="blue",height=2,width=15,font="Times 16 bold")
lbl.pack()
root.mainloop()

下面使用bind方法为Button绑定事件
唯一的区别就是不在需要绑定command,并且回调中需要传事件对象作为参数

from tkinter import*
# 回调的时候,传入event事件对象
def showGrilFriend(event):
    lbl.config(text="我的名字叫女朋友")
root=Tk()
root.title("Print GrilFriend")
root.geometry("300x200")
btn=Button(root,text="打印")
btn.pack(anchor=S)
# 将按钮的单击事件绑定到showGrilFriend回调函数上
btn.bind("<Button-1>",showGrilFriend) 
# 创建一个显示用的Label
lbl=Label(root,bg="yellow",fg="blue",height=2,width=15,font="Times 16 bold")
lbl.pack()
root.mainloop()

上代码:实时打印鼠标的位置

from tkinter import*
# 打印鼠标位置
def printMouseLocation(event):
    x=event.x
    y=event.y
    printInfo="鼠标位置  x:{},y:{}".format(x,y)
    var.set(printInfo)
root=Tk()
root.title("Print mouse location")
root.geometry("500x300")
x,y=0,0
var=StringVar()
text="鼠标位置  x:{},y:{}".format(x,y)
var.set(text) # 显示的初始位置为0,0
lbl=Label(root,textvariable=var)
lbl.pack(anchor=S,side=RIGHT,padx=10,pady=10)
# 为鼠标的移动事件绑定printMouseLocation方法
root.bind("<Motion>",printMouseLocation)
root.mainloop()

3. 取消事件绑定 unbind(event)

from tkinter import*
def callback(event):
    print("鼠标点击位置:",event.x,event.y)
# 取消事件订阅
def cancelEvent():
    frame.unbind("<Button-1>") # 取消绑定
    print("鼠标位置打印功能已经被取消")
root=Tk()
root.title("Mouse location")
btn=Button(root,text="取消事件",fg="black",command=cancelEvent)
btn.pack(side=TOP,padx=5,pady=5)
frame=Frame(root,width=500,height=280,bg="yellow")
frame.bind("<Button-1>",callback)
frame.pack()
root.mainloop()

4. 多事件绑定

from tkinter import*
# 单击事件回调
def clickCallback(event):
    print("this is click event")
# 单击并松开事件回调
def releaseCallback(event):
    print("this is release event")
root=Tk()
root.title("Multi event")
root.geometry("300x100")
btn=Button(root,text="Trigger")
btn.pack(anchor=W,padx=10,pady=10)
btn.bind("<Button-1>",clickCallback,add="+") # 绑定单击事件
btn.bind("<ButtonRelease-1>",releaseCallback,add="+") # 绑定松手事件
root.mainloop()

运行
在这里插入图片描述
当然,除了以上介绍的鼠标事件,键盘也是有事件的,大同小异,这里就不做介绍了
拜了个拜,如有疑问欢迎随时交流。。。。

1. 事件绑定 bind(event,handler)除了前面提到的Button中绑定command的事件之外,还有一种使用bind进行绑定事件的方式介绍几个常用的鼠标事件:&lt; Button-1&gt; 单击鼠标左键&lt; Button-3&gt; 单击鼠标右键&lt; Button-4&gt; 鼠标滚轮向上滚动&lt; Button-5&gt; 鼠标滚轮上下滚动&lt; Motion&gt; 鼠标移动-&lt; ButtonRelease-1&gt; 松开鼠标左键&lt; Dou command: command是控件的一个参数,如果使得command=函数,那么点击控件的时候将会触发函数 能够定义command的常见控件有: Button、Menu… 调用函数时,默认是没有参数传入的,如果要强制传入参数,可以考虑使用lambda from tkinter import * root=Tk() def prt(): print(
在用户界面程序, 菜单以图标和文字的方式展示可用选项. 用鼠标选择一个选项, 程序的某个行为既被触发. 这种行为通常包括比如 打开/保存文件, 退出程序 等功能. 上下文菜单是一种根据用户当前所在程序位置(上下文), 动态生成的菜单. 简单程序: from Tkinter import * root=Tk() root.geometry('300x100') M=Menu(root)
1、事件 事件可以有各种来源:包括用户触发的鼠标和键盘操作和窗口管理器触发的重绘事件(在多数情况下是由用户间接引起的)。Tkinter 提供一个强大的机制可以让你自由地处理事件,对于每个组件来说,你可以通过 bind() 方法将函数或方法绑定到具体的事件上。 2、事件序列 事件序列是以字符串的形式表示的,可以表示一个或多个相关联的事件事件序列使用以下语法描述: <modifier-type-detail> 事件序列是包含在尖括号(<...>)   说明:对于每个组件来说,可以通过bind()方法将函数或方法绑定到具体的事件上。 事件序列:   说明:用户需要使用bind()方法将具体的事件序列与自定义的方法绑定,时间序列是以字符串的形式表示的。   语法描述:     <modifier - type - dateil>     事件序列必...
组件对象的绑定,包含2种方法: 通过command属性绑定,适合简单不需要获取event对象的情况,例如:Button(root,text="登录",command=login); 通过bind()方法绑定,适合需要获取event对象的情况,例如:l=Label(text="A"); l.bind("<Button-1>",sendMessage); 组件类的绑定:通过调用对象的bind_class函数,将该组件类所有的组件绑
一个Tkinter主要跑在mainloop进程里。Events可能来自多个地方,比如按键,鼠标,或是系统事件Tkinter提供了丰富的方法来处理这些事件。对于每一个控件Widge,你都可以为其绑定方法function。widget.bind(event,handler)如果相应的event发生了,就会调用handler处理事件。举个例子: 捕获鼠标点击事件:from Tkinter impo
# @Email: slxxfl000@163.com # @Web: www.lzmath.cn # @Blog: https://blog.csdn.net/weixin_41810846 # @Date: 2019-08-20 17:20:12 # @Last Modified by: BlueS...