Reputation: 144
I have created a program in python(Tkinter) which takes username and password as input and a button which, when pressed, goes through a function which checks the username and password and further runs the program. I want that, rather than clicking the button, user presses the 'Enter' key and that does the function of the button. please help.
Upvotes: 3
Views: 10824
Reputation: 21
For me, the normal binding to a function did not work. Probably because I am using it inside a class, I used the Lambda function and it worked. Here's the code:
inp.bind('', lambda _: show())
Upvotes: 0
Reputation: 15878
You can bind the <Return>
event on the Entry
widget with some method (which will do what you want):
# binding <Return> event
import tkinter as tk
import tkinter.messagebox as msg
def show(event=None): # handler
msg.showinfo('name', 'Your name is ' + inp.get())
m = tk.Tk()
prompt = tk.Label(m, text='Name: ')
prompt.pack(fill='x', side='left')
inp = tk.Entry(m)
inp.bind('<Return>', show) # binding the Return event with an handler
inp.pack(fill='x', side='left')
ok = tk.Button(m, text='GO', command=show)
ok.pack(fill='x', side='left')
m.mainloop()
If you want to know more about events and bindings, see this effbot's page, which is quite easy to understand.
Upvotes: 4
Reputation: 1695
I believe you'll want to bind the key to your handler: frame.bind('<Return>', some_handler)
.
Upvotes: 0