Tkinter

How to Bind The Enter key to a Function in Tkinter

In this tutorial, we are going to see how to bind the enter key to a function in Tkinter Python. Clicking the Enter key is an event, and we can bind functions to this event so that the event triggers a given function.

widget.bind(event, handler)

If the event occurs, it will automatically trigger the handler.
 

 

How to Bind The Enter key to a Function in Tkinter
import tkinter as tk

gui = tk.Tk()
gui.geometry("200x200")

def myFunction(event):
    label["text"] = "You have pressed Enter"

gui.bind('<Return>', myFunction)

label = tk.Label(gui, text="")
label.pack()

gui.mainloop()

Output:


 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *