Applications should perform actions whenever certain events occur (a key or mouse button is pressed, ...). User should write the code (an event handler) that will be executed in response to an event.
The 'mainloop()' method maintains a list of events that have occurred and it runs an event handler any time a new event is added to the event list.
# event1.py import tkinter as tk root = tk.Tk() def handle_keypress(event): """Print the character associated to the key pressed.""" print("The key {} was pressed!".format(event.char)) def handle_keyrelease(event): """Print the character associated to the key released.""" print("The key {} was released!".format(event.char)) # no char def handle_click(event): print("Clicked at {} {}".format(event.x, event.y)) # Bind keypress event to handle_keypress(). # The output is printed to stdout. #root.bind(event_string, event_handler) #root.bind("<Key>", handle_keypress) #root.bind("<Key>", lambda event: print(event.char)) root.bind("<KeyPress>", handle_keypress) # the same as "<Key>" root.bind("<KeyRelease>", handle_keyrelease) root.bind("<Return>", lambda event: print("Return was pressed")) root.bind("<Escape>", lambda event: print("Escape was pressed")) root.bind("<BackSpace>", lambda event: print("BackSpace was pressed")) root.bind("<Tab>", lambda event: print("Tab was pressed")) # Cursors. root.bind("<Up>", lambda event: print("Up was pressed")) root.bind("<Down>", lambda event: print("Down was pressed")) root.bind("<Left>", lambda event: print("Left was pressed")) root.bind("<Right>", lambda event: print("Right was pressed")) # Mouse clics. root.bind("<Button-1>", handle_click) # the left mouse button root.bind("<Button-2>", handle_click) # the middle mouse button root.bind("<Button-3>", handle_click) # the right mouse button root.bind("<Button-4>", handle_click) # scroll up root.bind("<Button-5>", handle_click) # scroll down #root.bind("<Button-3>", lambda event: print(event.char)) # prints ?? root.bind("<Double-1>", lambda event: print("left double-click")) root.bind("<Double-2>", lambda event: print("middle double-click")) root.bind("<Double-3>", lambda event: print("right double-click")) root.bind("<Motion>", lambda event: print("a mouse pointer is moved")) root.bind("<Configure>", lambda event: print("a window is resized|repositioned|...")) root.bind("<Destroy>", lambda event: print("a window is destroyed")) root.bind("<Enter>", lambda event: print("mouse enters a window")) root.bind("<Leave>", lambda event: print("mouse leaves a window")) root.bind("<Map>", lambda event: print("a window is opened")) root.bind("<Unmap>", lambda event: print("a window is iconified")) root.bind("<FocusIn>", lambda event: print("a window gains focus")) root.bind("<FocusOut>", lambda event: print("a window loses focus")) root.mainloop() # run the tkinter event loop
# Modifiers. "<Motion>" and "<B1-Motion>" (moving the mouse with the left button pressed) "<KeyPress>" and "<KeyPress-a>" (pressing the "a" key only)
# Synonyms. "<ButtonPress-1>", "<Button-1>", "<1>" "<Double-ButtonPress-1>", "<Double-1>" "<KeyPress-a>", "<Key-a>"
Every Button widget has a 'command' attribute that you can assign to a function. Whenever the button is pressed, the function is executed.
# event2.py import tkinter as tk def increase(): value = int(label["text"]) label["text"] = "{}".format(value + 1) def decrease(): value = int(label["text"]) label["text"] = "{}".format(value - 1) root = tk.Tk() root.rowconfigure(0, minsize=100, weight=1) root.columnconfigure(0, minsize=100, weight=1) root.columnconfigure(1, minsize=100, weight=1) root.columnconfigure(2, minsize=100, weight=1) button_decrease = tk.Button(root, text="-", command=decrease) # command=lambda: label.config(text=str(int(label["text"])-1))) button_decrease.grid(row=0, column=0, sticky=tk.NSEW) label = tk.Label(root, text="0") label.grid(row=0, column=1) button_increase = tk.Button(root, text="+", command=increase) # command=lambda: label.config(text=str(int(label["text"])+1))) button_increase.grid(row=0, column=2, sticky=tk.NSEW) root.mainloop() # run the tkinter event loop