Python Tkinter-Event

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()
image.png

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()
image.png

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()
image.png

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()
image.png

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()
image.png

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()
image.png

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()
image.png

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()
image.png

响应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()
image.png

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()
image.png

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()
image.png

你可能感兴趣的:(Python Tkinter-Event)