#coding=utf-8
'''组件对象的绑定
1.通过command属性绑定(适合简单不需获取event对象) Button(root,text="登录",command=login)
2.通过bind()方法绑定(适合需要获取evnet对象) c1 = Canvas(); c1.bind("<Button-1>",drawLine)
组件类的绑定
调用对象的bind_class函数,将该组件类所有的组件绑定事件:
w.bind_class("Widget","event",eventhanler) 比如:btn01.bind_class("Button","<Button-1>",func)
'''
#多种事件绑定方式汇总
from tkinter import *
root = Tk()
root.geometry("270x30")
def mouseTest1(event):
print("bind()方式绑定,可获取event对象")
print(event.widget)
def mouseTest2(a, b):
print("a={0}, b={1}".format(a, b))
print("command方式绑定,不能直接获取event对象")
def mouseTest3(event):
print("钟呢呢右键单击事件,绑定给所有按钮啦!!")
print(event.widget)
b1 = Button(root, text="测试bind()绑定") #第一个按钮b1
b1.pack(side="left")
b1.bind("<Button-1>", mouseTest1) #bind方式绑定事件
b2 = Button(root, text="测试command绑定", command=lambda: mouseTest2("xiannv", "zhongnene"))#第二个按钮b2 通过lambda直接传参
b2.pack(side="left")
b1.bind_class("<Button>", "<Button-2>", mouseTest3)#给所有Button按钮都绑定右键单击事件<Button-2>
root.mainloop()
<button-2>为鼠标中键,然而单击鼠标中键没反应
看视频高老师是单击的鼠标右键 我也试了但也没反应