1.点击

from tkinter import *

root=Tk()
def printCoords(event):
    print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('<Button-1>',printCoords)

bt2=Button(root,text='middle button')
bt2.bind('<Button-2>',printCoords)

bt3=Button(root,text='rightmost button')
bt3.bind('<Button-3>',printCoords)

bt4=Button(root,text='double button')
bt4.bind('<Double-Button-1>',printCoords)

bt5=Button(root,text='triple button')
bt5.bind('<Triple-Button-1>',printCoords)

bt1.grid()
bt2.grid()
bt3.grid()
bt4.grid()
bt5.grid()

root.mainloop()

Python Tkinter-Event-LMLPHP

2.移动

from tkinter import *

root=Tk()
def printCoords(event):
    print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('<B1-Motion>',printCoords)

bt2=Button(root,text='middle button')
bt2.bind('<B2-Motion>',printCoords)

bt3=Button(root,text='rightmost button')
bt3.bind('<B3-Motion>',printCoords)

bt1.grid()
bt2.grid()
bt3.grid()

root.mainloop()

Python Tkinter-Event-LMLPHP

3.释放

from tkinter import *

root=Tk()
def printCoords(event):
    print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('<ButtonRelease-1>',printCoords)

bt2=Button(root,text='middle button')
bt2.bind('<ButtonRelease-2>',printCoords)

bt3=Button(root,text='rightmost button')
bt3.bind('<ButtonRelease-3>',printCoords)

bt1.grid()
bt2.grid()
bt3.grid()

root.mainloop()

Python Tkinter-Event-LMLPHP

4.进入

from tkinter import *

root=Tk()
def printCoords(event):
    print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('<Enter>',printCoords)

bt1.grid()

root.mainloop()

Python Tkinter-Event-LMLPHP

5.离开

from tkinter import *

root=Tk()
def printCoords(event):
    print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('<Leave>',printCoords)

bt1.grid()

root.mainloop()

Python Tkinter-Event-LMLPHP

6.响应特殊键

from tkinter import *

root=Tk()
def printCoords(event):
    print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('<BackSpace>',printCoords)

bt2=Button(root,text='middle button')
bt2.bind('<Return>',printCoords)

bt3=Button(root,text='rightmost button')
bt3.bind('<F5>',printCoords)

bt1.focus_set()
bt1.grid()
bt2.grid()
bt3.grid()

root.mainloop()

Python Tkinter-Event-LMLPHP

7.响应所有按键

from tkinter import *

root=Tk()
def printCoords(event):
    print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('<Key>',printCoords)

bt1.focus_set()
bt1.grid()

root.mainloop()

Python Tkinter-Event-LMLPHP

8.响应指定按键

from tkinter import *

root=Tk()
def printCoords(event):
    print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('a',printCoords)

bt2=Button(root,text='middle button')
bt2.bind('<space>',printCoords)

bt3=Button(root,text='rightmost button')
bt3.bind('<less>',printCoords)

bt1.focus_set()
bt1.grid()
bt2.grid()
bt3.grid()

root.mainloop()

Python Tkinter-Event-LMLPHP

响应a

空格

小于号

9.响应组合键

from tkinter import *

root=Tk()
def printCoords(event):
    print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('<Control-Alt-c>',printCoords)

bt1.focus_set()
bt1.grid()

root.mainloop()

Python Tkinter-Event-LMLPHP

10.改变组件大小事件

from tkinter import *

root=Tk()
def printCoords(event):
    print(event.width,event.height)
bt1=Button(root,text='leftmost button')
bt1.bind('<Configure>',printCoords)

bt1.focus_set()
bt1.grid()

root.mainloop()

Python Tkinter-Event-LMLPHP

11.两个事件绑定到一个控件

from tkinter import *

root=Tk()
def printEvent(event):
    print('<Key>',event.keycode)
def printReturn(event):
    print('<Return>',event.keycode)

root.bind('<Key>',printEvent)
root.bind('<Return>',printReturn)

root.mainloop()

Python Tkinter-Event-LMLPHP

05-23 05:02