When you need to interact with the user in the window created with Python’s module tkinter, you need to use the bind method, if you are not using a button that has his own command method built in. Let’s explor the different keywords used in the method bind for the different event that the user may activate on a widget.
Enter event is <Return>
To call a function or a method when you press enter while a widget is focused, you will need to use the keyword <Return> into apostrophes, like in this example.
After you imported tkinter like this:
import tkinter as tk # for python 2: import Tkinter as tk
And you create a window
root = tk.Tk() root.mainloop()
Imagine you create an Entry widget like this (between root = tk.Tk() and root.mainloop():
entry = tk.Entry(root) entry.pack()
To make something happen when you hit enter you write this and you’re done.
self.entry.bind("<Return>", print("Hello World")
event | keyword |
press Enter | <Return> |