Home > Enterprise >  Alternative Process in Returning a User Input from a Tkinter Entry
Alternative Process in Returning a User Input from a Tkinter Entry

Time:10-15

Is there Anyway to Bind the Key "Enter" to the button of a tkinter entry? For example I was to query a value from a database and entered the Value to the textbox, but i have to press the button on the screen rather than pressing the enter button on the keyboard. Is there anyway to do it?

CodePudding user response:

Solution

For binding the "enter" key, just use the win.bind('<Return>', command). Here is an example for your case (using label):

#Import the tkinter library
from tkinter import *

#Create an instance of tkinter Tk
win = Tk()

#Set the geometry
win.geometry("650x250")

#Event Handler function
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()

Other Key Bindings

Most keys have their own name as the name used while binding. But some keys have different key-binds. A list of key-binds and events in tkinter can be found here (These can be used using the tkinter function tkinter.bind('<bind-code>', handler))

  • Related