For each component, a function or method can be bound to a specific event through the bind () method, and Tkinter will invoke handler () when the condition is met.
Syntax: Widget.bind (Event,handler)
One, the mouse event example
Left mouse button Event
From Tkinter import *
root = Tk ()
def callback (event):
#相对于指定窗口坐标, X_root and y_root relative to the entire window coordinate.
# (upper left corner coordinate is (0,0))
print (' present position: ', event.x,event.y)
frame = FRAME (Root,width = 200,height = 200,BG = ' White ') c6/> #bind () method binds the left mouse button click and callback function
frame.bind (' <Button-1> ', callback)
Frame.pack ()
Mainloop ()
Mouse Continuous Trigger Event
From Tkinter import *
root = Tk ()
def callback (event):
print (' Current position: ', event.x,event.y)
frame = Frame (root,width = 200,height = 200,BG = ' white ')
frame.bind (' <Motion> ', callback)
Frame.pack ()
Mainloop ()
Second, keyboard event examples
from tkinter import * root = Tk () def callback (event): #char is a property of the event, the corresponding character of the key (key-specific attribute) Print (' Click Position: ', Event.char ') frame = frame (Root,width = 200,height =) frame.bind (' <Key> ', callback) #focus_set ()
The key is to get the focus, which is required for the key type. Frame.focus_set () Frame.pack () Mainloop ()