Python全系列 教程
3567个小节阅读:5929.3k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
组件对象的绑定
- 通过command属性绑定(适合简单不需获取event对象)
Button(root,text=”登录”,command=login)
- 通过bind()方法绑定(适合需要获取event对象)
c1 = Canvas(); c1.bind(“
”,drawLine)
组件类的绑定
调用对象的bind_class函数,将该组件类所有的组件绑定事件:
w.bind_class(“Widget”,”event”,eventhanler)
比如:btn01.bind_class(“Button”,”
【示例】多种事件绑定方式总结
xxxxxxxxxx
# coding=utf-8
# 多种事件绑定方式汇总
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.pack(side="left")
# bind方式绑定事件
b1.bind("" , mouseTest1)
# command属性直接绑定事件
b2 = Button(root, text="测试command2",
command=lambda: mouseTest2("gaoqi", "xixi"))
b2.pack(side="left")
# 给所有Button按钮都绑定右键单击事件
b1.bind_class("Button", "" , mouseTest3)
root.mainloop()