Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I bind the enter key to a function in Tkinter?
Pressing a key and handling some operation with the key is an event that can be triggered through a button. We can bind the key event using the Binding method in a tkinter application.
Whenever the key will be triggered, it will call a handler that will raise the specific operation for the key event.
If we want to trigger the Enter key with the bind function, we will use the bind('<Key>', Handler) method. For Enter Key, we use bind('<Return>', Handler) function.
Example
#Import the tkinter library
from tkinter import *
#Create an instance of tkinter frame
win = Tk()
#Set the geometry
win.geometry("650x250")
def handler(e):
label= Label(win, text= "You Pressed Enter")
label.pack()
#Create a Label
Label(win, text= "Press Enter on the Keyboard", font= ('Helvetica bold', 14)).pack(pady=20)
#Bind the Enter Key to Call an event
win.bind('<Return>',handler)
win.mainloop()
Output
It will display the following window −

Now, if we will press "Enter" on the keyboard, it will display “You Pressed Enter”.

Advertisements