python_tkinter事件类型详情
作者:手可摘星辰。 时间:2021-11-29 10:09:30
1.事件绑定函数
事件绑定函数有三个如下:
组件.bind('事件类型',事件函数)
为一个组件绑定一个操作
组件.bind_class('组件类型','事件类型',事件函数)
为一个类组件绑定一个操作
组件.bind_all('事件类型',事件函数)
为所有组件绑定一个操作(所有操作都会当作对主界面的操作)
2.事件类型
3.事件对象
事件绑定的案例1:
鼠标进入组件变红,离开组件变白:
# 单行文本输入框
entry = tkinter.Entry(root)
entry.pack()
# 事件函数
def changered(eventobj):
# 通过事件对象获取得到组件
eventobj.widget['bg'] = 'red'#鼠标进入组件变红
def wdc(eventobj):
eventobj.widget['bg'] = 'white'#鼠标离开组件变白
# 事件绑定
entry.bind('<Enter>',changered)
entry.bind('<Leave>',wdc)
注意:通过绑定函数操作,对应的事件函数,必须有形参接受事件对象。
事件函数必须在绑定之前定义
事件绑定的案例2:
键盘快捷键触发事件:按快捷键改变窗口背景色
import tkinter
# 创建主窗口
root = tkinter.Tk()
# 设置窗口大小
root.minsize(300,200)
# 变红色
def changer(eventobj):
eventobj.widget['bg'] = 'red'
# 按Ctrl + r窗口变红色:两条命令 R和r都行
root.bind('<Control-KeyPress-r>',changer)
root.bind('<Control-KeyPress-R>',changer)
# 加入消息循环
root.mainloop()
事件绑定的案例3:(为一类组件绑定事件)
import tkinter
# 创建主窗口
root = tkinter.Tk()
# 设置窗口大小
root.minsize(300,200)
# 按钮1
btn1 = tkinter.Button(root,text = '1')
btn1.place(x = 20,y = 20,width = 40,height = 40)
# 按钮2
btn2 = tkinter.Button(root,text = '2')
btn2.place(x = 80,y = 20,width = 40,height = 40)
# 按钮3
btn3 = tkinter.Button(root,text = '3')
btn3.place(x = 140,y = 20,width = 40,height = 40)
# 按钮4
btn4 = tkinter.Button(root,text = '4')
btn4.place(x = 20,y = 80,width = 40,height = 40)
# 按钮5
btn5 = tkinter.Button(root,text = '5')
btn5.place(x = 80,y = 80,width = 40,height = 40)
# 按钮6
btn6 = tkinter.Button(root,text = '6')
btn6.place(x = 140,y = 80,width = 40,height = 40)
def changebg(wdc):
# 鼠标放到按钮上按钮变红
wdc.widget['bg'] = 'red'
def changebg1(wdc):
# 鼠标离开按钮上按钮变白
wdc.widget['bg'] = 'white'
# 绑定按钮鼠标进入事件
btn1.bind_class('Button','<Enter>',changebg)
btn1.bind_class('Button','<Leave>',changebg1)
# 加入消息循环
root.mainloop()
事件绑定的案例4:(为所有组件绑定事件)
鼠标点击所有组件,Entry组件背景颜色都会变红:
import tkinter
# 创建主窗口
root = tkinter.Tk()
# 设置窗口大小
root.minsize(300,200)
# 按钮
btn1 = tkinter.Button(root,text = '###############')
btn1.pack()
# 输入框
entry = tkinter.Entry(root)
entry.pack()
# 多行输入框
text = tkinter.Text(root,width = 20,height = 5)
text.pack()
# 函数
def changeentry(e):
# 鼠标点击任意组件,Entry组件都会变红
entry['bg'] = 'red'
# 事件绑定
btn1.bind_all('<Button-1>',changeentry)
# 加入消息循环
root.mainloop()
来源:https://www.cnblogs.com/wangdianchao/p/11555130.html
标签:python,kinte,r事件,类型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python多线程编程之threading模块详解
2023-12-28 07:52:59
![](https://img.aspxhome.com/file/2023/5/121095_0s.png)
解决oracle用户连接失败的解决方法
2011-01-04 19:35:00
flask route对协议作用及设计思路
2023-01-19 13:21:34
简单的在线调试服务端js代码的asp源码
2008-04-23 13:30:00
TensorFlow tf.nn.max_pool实现池化操作方式
2021-08-20 20:36:45
![](https://img.aspxhome.com/file/2023/1/130541_0s.jpg)
Python图像处理之直线和曲线的拟合与绘制【curve_fit()应用】
2021-01-28 10:30:49
![](https://img.aspxhome.com/file/2023/3/67353_0s.png)
pytest中配置文件pytest.ini使用
2021-01-22 17:04:02
如何将yolov5中的PANet层改为BiFPN详析
2023-08-12 18:08:38
![](https://img.aspxhome.com/file/2023/9/64349_0s.jpg)
关于Python 多重继承时metaclass conflict问题解决与原理探究
2022-04-17 04:50:10
ASP如何使用CDONTS来发送电子邮件?
2010-06-05 12:35:00
python机器学习之随机森林(七)
2023-10-20 00:25:13
Python爬虫实战之使用Scrapy爬取豆瓣图片
2023-06-08 10:56:20
![](https://img.aspxhome.com/file/2023/5/97665_0s.png)
几个好用的Asp自定义函数
2007-09-26 14:28:00
django 通过URL访问上传的文件方法
2022-09-02 22:03:59
![](https://img.aspxhome.com/file/2023/0/97190_0s.jpg)
python celery分布式任务队列的使用详解
2021-04-22 18:17:56
![](https://img.aspxhome.com/file/2023/1/97641_0s.png)
Python用 matplotlib 绘制柱状图
2023-01-22 01:20:50
![](https://img.aspxhome.com/file/2023/6/123796_0s.png)
YUI Compressor快速使用指南
2011-06-27 20:07:30
Python中多个数组行合并及列合并的方法总结
2021-10-08 11:00:23
发一新浪招聘的图片滚动控制JS效果
2011-08-10 19:17:25
Web开发者的百科全书——Google DocType
2008-07-03 13:06:00