1.点击
from tkinter import *
root=Tk()
def printCoords(event):
print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('',printCoords)
bt2=Button(root,text='middle button')
bt2.bind('',printCoords)
bt3=Button(root,text='rightmost button')
bt3.bind('',printCoords)
bt4=Button(root,text='double button')
bt4.bind('',printCoords)
bt5=Button(root,text='triple button')
bt5.bind('',printCoords)
bt1.grid()
bt2.grid()
bt3.grid()
bt4.grid()
bt5.grid()
root.mainloop()
2.移动
from tkinter import *
root=Tk()
def printCoords(event):
print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('',printCoords)
bt2=Button(root,text='middle button')
bt2.bind('',printCoords)
bt3=Button(root,text='rightmost button')
bt3.bind('',printCoords)
bt1.grid()
bt2.grid()
bt3.grid()
root.mainloop()
3.释放
from tkinter import *
root=Tk()
def printCoords(event):
print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('',printCoords)
bt2=Button(root,text='middle button')
bt2.bind('',printCoords)
bt3=Button(root,text='rightmost button')
bt3.bind('',printCoords)
bt1.grid()
bt2.grid()
bt3.grid()
root.mainloop()
4.进入
from tkinter import *
root=Tk()
def printCoords(event):
print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('',printCoords)
bt1.grid()
root.mainloop()
5.离开
from tkinter import *
root=Tk()
def printCoords(event):
print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('',printCoords)
bt1.grid()
root.mainloop()
6.响应特殊键
from tkinter import *
root=Tk()
def printCoords(event):
print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('',printCoords)
bt2=Button(root,text='middle button')
bt2.bind('',printCoords)
bt3=Button(root,text='rightmost button')
bt3.bind('',printCoords)
bt1.focus_set()
bt1.grid()
bt2.grid()
bt3.grid()
root.mainloop()
7.响应所有按键
from tkinter import *
root=Tk()
def printCoords(event):
print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('',printCoords)
bt1.focus_set()
bt1.grid()
root.mainloop()
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('',printCoords)
bt3=Button(root,text='rightmost button')
bt3.bind('',printCoords)
bt1.focus_set()
bt1.grid()
bt2.grid()
bt3.grid()
root.mainloop()
响应a
空格
小于号
9.响应组合键
from tkinter import *
root=Tk()
def printCoords(event):
print(event.x,event.y)
bt1=Button(root,text='leftmost button')
bt1.bind('',printCoords)
bt1.focus_set()
bt1.grid()
root.mainloop()
10.改变组件大小事件
from tkinter import *
root=Tk()
def printCoords(event):
print(event.width,event.height)
bt1=Button(root,text='leftmost button')
bt1.bind('',printCoords)
bt1.focus_set()
bt1.grid()
root.mainloop()
11.两个事件绑定到一个控件
from tkinter import *
root=Tk()
def printEvent(event):
print('',event.keycode)
def printReturn(event):
print('',event.keycode)
root.bind('',printEvent)
root.bind('',printReturn)
root.mainloop()