Reputation: 4689
I have a skeleton of a program that I want to use:
from tkinter import *
import urllib
import urllib.request
import xml.etree.ElementTree as ET
root = Tk()
def program():
print('Hello')
tex=Text(root)
tex.pack(side='right')
inputfield = Entry(root)
inputfield.pack(side='bottom')
text = inputfield.get()
but = Button(root,text="Enter", command = program)
but.pack(side='bottom')
root.mainloop()
Alright so recapping, the program is just a frame with a text field, input field and a button that says Enter
. I want to call the program the button calls without actually pressing the button. I want to input the text in the input field and press Enter on my keyboard to call the function.
Is that possible through tkinter?
Upvotes: 2
Views: 3884
Reputation: 20689
Yes, it is possible. You only have to bind the Entry widget with the event <Return>
:
inputfield.bind('<Return>', lambda _: program())
Since the callback function used in bind
receives one argument (a Tkinter event), you cannot use the reference to program
directly. So instead of changing the definition of the function, you can use a lambda and name the first argument as _
, a common name for "don't care" variables.
Upvotes: 6