1 #鼠标事件绑定 2 from tkinter import * 3 root = Tk() 4 5 def callback(event): 6 print('位置是:',event.x,event.y) 7 8 frame = Frame(root,width=200,height=200) 9 frame.pack() 10 frame.bind('<Button-1>',callback) 11 12 mainloop()
1 #键盘事件绑定 2 from tkinter import * 3 root = Tk() 4 5 def callback(event): 6 print(event.char) 7 8 frame = Frame(root,width=200,height=200) 9 frame.bind('<Key>',callback) 10 frame.focus_set() 11 frame.pack() 12 13 mainloop()
1 #Motion当鼠标进入区域就会触发事件 2 from tkinter import * 3 root = Tk() 4 5 def callback(event): 6 print('位置是',event.x,event.y) 7 8 frame = Frame(root,width=200,height=200) 9 frame.bind('<Motion>',callback) 10 frame.focus_set() 11 frame.pack() 12 13 mainloop()