前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python tkinter 绑定事件合集

python tkinter 绑定事件合集

作者头像
用户5760343
发布2022-05-13 11:07:46
5760
发布2022-05-13 11:07:46
举报
文章被收录于专栏:sktj

from tkinter import *

def showPosEvent(event): print('Widget=%s X=%s Y=%s' % (event.widget, event.x, event.y))

def showAllEvent(event): print(event) for attr in dir(event): if not attr.startswith('__'): print(attr, '=>', getattr(event, attr))

def onKeyPress(event): print('Got key press:', event.char)

def onArrowKey(event): print('Got up arrow key press')

def onReturnKey(event): print('Got return key press')

def onLeftClick(event): print('Got left mouse button click:', end=' ') showPosEvent(event)

def onRightClick(event): print('Got right mouse button click:', end=' ') showPosEvent(event)

def onMiddleClick(event): print('Got middle mouse button click:', end=' ') showPosEvent(event) showAllEvent(event)

def onLeftDrag(event): print('Got left mouse button drag:', end=' ') showPosEvent(event)

def onDoubleLeftClick(event): print('Got double left mouse click', end=' ') showPosEvent(event) tkroot.quit()

tkroot = Tk() labelfont = ('courier', 20, 'bold') # family, size, style widget = Label(tkroot, text='Hello bind world') widget.config(bg='red', font=labelfont) # red background, large font widget.config(height=5, width=20) # initial size: lines,chars widget.pack(expand=YES, fill=BOTH)

widget.bind('<Button-1>', onLeftClick) # mouse button clicks widget.bind('<Button-3>', onRightClick) widget.bind('<Button-2>', onMiddleClick) # middle=both on some mice widget.bind('<Double-1>', onDoubleLeftClick) # click left twice widget.bind('<B1-Motion>', onLeftDrag) # click left and move

widget.bind('<KeyPress>', onKeyPress) # all keyboard presses widget.bind('<Up>', onArrowKey) # arrow button pressed widget.bind('<Return>', onReturnKey) # return/enter key pressed widget.focus() # or bind keypress to tkroot tkroot.title('Click Me') tkroot.mainloop()

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档