0

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?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Koshi
  • 31
  • 4

1 Answers1

0

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))

Pycoder
  • 92
  • 9
  • Different modules such as `keyboard` and `pygame` have their own different key-binds for special keys so make sure to check their key-binds. – Pycoder Oct 16 '22 at 06:44