1、绑定鼠标事件并获取事件属性
# -*- encoding=utf-8 -*- import tkinter from tkinter import * def left_mouse_down(event): print('鼠标左键按下') # 事件的属性 widget = event.widget print('触发事件的组件:{}'.format(widget)) print('组件颜色:{}'.format(widget.cget('bg'))) widget_x = event.x # 相对于组件的横坐标x print('相对于组件的横坐标:{}'.format(widget_x)) widget_y = event.y # 相对于组件的纵坐标y print('相对于组件的纵坐标:{}'.format(widget_y)) x_root = event.x_root # 相对于屏幕的左上角的横坐标 print('相对于屏幕的左上角的横坐标:{}'.format(x_root)) y_root = event.y_root # 相对于屏幕的左上角的纵坐标 print('相对于屏幕的左上角的纵坐标:{}'.format(y_root)) def left_mouse_up(event): print('鼠标左键释放') def moving_mouse(event): print('鼠标左键按下并移动') def moving_into(event): print('鼠标进入') def moving_out(event): print('鼠标移出') def right_mouse_down(event): print('鼠标右键按下') def right_mouse_up(event): print('鼠标右键释放') def pulley_up(event): print('滑轮向上滚动') def focus(event): print('聚焦事件') def unfocus(event): print('失焦事件') if __name__ == '__main__': win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 500 height = 300 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 label = Label(text='标签', relief='g', font=('黑体', 20)) label.pack(pady=10) label.bind('<Button-1>', left_mouse_down) # 鼠标左键按下 label.bind('<ButtonRelease-1>', left_mouse_up) # 鼠标左键释放 label.bind('<Button-3>', right_mouse_down) # 鼠标右键按下 label.bind('<ButtonRelease-3>', right_mouse_up) # 鼠标右键释放 label.bind('<B1-Motion>', moving_mouse) # 鼠标左键按下并移动 label.bind('<Enter>', moving_into) # 鼠标移入事件 label.bind('<Leave>', moving_out) # 鼠标移出事件 label.bind('<FocusIn>', focus) # 聚焦事件 label.bind('<FocusOut>', unfocus) # 失焦事件 label.focus_set() # 直接聚焦 Entry().pack() win.mainloop()
2、绑定键盘事件并获取事件属性
# -*- encoding=utf-8 -*- import tkinter from tkinter import * def keyboard_event(event): char = event.char print('回车 char:{}'.format(char)) key_code = event.keycode print('回车 key code:{}'.format(key_code)) def entry_enter(event): print('输入的内容为:' + entry.get()) def shift_f(event): print('SHIFT + F') print(event.char) print(event.keycode) def num_lock(event): print('num_lock') print(event.char) print(event.keycode) if __name__ == '__main__': win = tkinter.Tk() # 窗口 win.title('南风丶轻语') # 标题 screenwidth = win.winfo_screenwidth() # 屏幕宽度 screenheight = win.winfo_screenheight() # 屏幕高度 width = 500 height = 300 x = int((screenwidth - width) / 2) y = int((screenheight - height) / 2) win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置 label = Label(text='标签', relief='g', font=('黑体', 20)) label.pack(pady=10) label.focus_set() label.bind('<Return>', keyboard_event) # 按下回车 label.bind('<Shift F>', shift_f) label.bind('<Num_Lock>', num_lock) entry = Entry() entry.pack() entry.bind('<Return>', entry_enter) # 按下回车 win.mainloop()
以上就是Python tkinter之Bind(绑定事件)的使用示例的详细内容,更多关于python tkinter Bind(绑定事件)的资料请关注其它相关文章!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金钱帮资源网 Copyright www.kbjia.com
暂无“Python tkinter之Bind(绑定事件)的使用示例”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年10月10日
2024年10月10日
- 《艾诺提亚:失落之歌》:令人失望的意大利特色试验品
- 《蛋仔派对》斯派克皮肤图鉴
- 黑神话悟空九种美酒在哪获得 黑神话悟空九种酒位置一览
- 黑神话悟空全丹药配怎么获取 黑神话悟空全丹药配方获取方式介绍
- 梁汉文2014-听后感[天凯唱片][WAV+CUE]
- 徐小明1984-剑胆琴心(LP版)[文志][WAV+CUE]
- 韩红-精选(庆功版)[WAV整轨]
- 《暗喻幻想:ReFantazio》获IGN法国满分评价:完美的新标杆之作
- 《暗喻幻想》开分后 开发者小姐姐感动到热泪盈眶
- 数毛社:转向虚幻5后 《光环》将更容易登陆PS5平台
- 裘海正.1997-看不见的温柔【飞鹰】【WAV+CUE】
- 废五金.1998-废五金【SONY】【WAV+CUE】
- 群星.1996-各自各精彩【上华】【WAV+CUE】
- 《终极幸运之塔》评测:藏在童年回忆里爬塔佳作
- 《战神5》PC版评测:轻推W缓慢前行