How do I set a specific action to happen when the "enter" key on my keyboard is pressed in python
<p>I am making a python calculator with GUI for school. </p>

<p>I have got some basic code from the internet and I have to customize it by changing things around. So far I have added a <code>DEL</code> button, a <code>^2</code> button and a <code>sqrt()</code> button. </p>

<p>I now want that if I type in an equation on my keyboard, e.g. "2*4", and press <kbd>Enter</kbd> it will simulate as pressing the equals button. I am having trouble finding out how to get python to register me clicking the <kbd>Enter</kbd> and then give me an answer. </p>

<p>This is the code:</p>

<pre><code>from __future__ import division
from math import *
from functools import partial
try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk
class MyApp(tk.Tk):
    def __init__(self):
        # the root will be self
        tk.Tk.__init__(self)
        self.title("Magic")
        # use width x height + x_offset + y_offset (no spaces!)
        #self.geometry("300x150+150+50")
        # or set x, y position only
        self.geometry("+150+50")
        self.memory = 0
        self.create_widgets()
    def create_widgets(self):
        # this also shows the calculator's button layout
        btn_list = [
        '7',  '8',  '9',  '*',  'AC',
        '4',  '5',  '6',  '/',  'xÂ²',
        '1',  '2',  '3',  '-',  'âx',
        '0',  '.',  '=',  '+',  'DEL' ]
        rel = 'ridge'
        # create all buttons with a loop
        r = 1
        c = 0
        for b in btn_list:
            # partial takes care of function and argument
            cmd = partial(self.calculate, b)
            tk.Button(self, text=b, width=5, relief=rel,
                command=cmd).grid(row=r, column=c)
            c += 1
            if c &gt; 4:
                c = 0
                r += 1
        # use an Entry widget for an editable display
        self.entry = tk.Entry(self, width=37, bg="white")
        self.entry.grid(row=0, column=0, columnspan=5)

    def undo():
            new_string = whole_string[:-1]
            print(new_string)
            clear_all()
            display.insert(0, new_string)

    def calculate(self, key):
        if key == '=':
            # here comes the calculation part
            try:
                result = eval(self.entry.get())
                self.entry.insert(tk.END, " = " + str(result))
            except:
                self.entry.insert(tk.END, "")
        elif key == 'AC':
            self.entry.delete(0, tk.END)
        elif key == 'xÂ²':
            self.entry.insert(tk.END, "**")
            # extract the result
        elif key == 'âx':
            self.memory = self.entry.get()
            self.entry.delete(0, tk.END)
            self.entry.insert(tk.END, "sqrt(")
            self.entry.insert(tk.END, self.memory)
            self.entry.insert(tk.END, ")")
        elif key == 'DEL':
            self.memory = self.entry.get()
            self.entry.delete(0, tk.END)
            self.entry.insert(tk.END, self.memory[:-1])

        else:# previous calculation has been done, clear entry
            if '=' in self.entry.get():
                self.entry.delete(0, tk.END)
            self.entry.insert(tk.END, key)

app = MyApp()
app.mainloop()
</code></pre>

<p>You can use <code>bind()</code> to assign function to <code>Entry</code> which will be executed when you press <code>Enter</code></p>

<p>Example:</p>

<pre><code>import tkinter as tk

def on_return(event):
    print('keycode:', event.keycode)
    print('text in entry:', event.widget.get())

root = tk.Tk()

e = tk.Entry(root)
e.pack()
e.bind('&lt;Return&gt;', on_return)   # standard Enter
e.bind('&lt;KP_Enter&gt;', on_return) # KeyPad Enter

root.mainloop()
</code></pre>

<p>In your code it can be - for test</p>

<pre><code>self.entry = tk.Entry(self, width=37, bg="white")
self.entry.grid(row=0, column=0, columnspan=5)

self.entry.bind('&lt;Return&gt;', lambda event:print("ENTER:", event.widget.get()))
self.entry.bind('&lt;KP_Enter&gt;', lambda event:print("ENTER:", event.widget.get()))
</code></pre>

<p>If you have class method <code>def on_return(self, event):</code> then </p>

<pre><code>self.entry.bind('&lt;Return&gt;', self.on_return)
self.entry.bind('&lt;KP_Enter&gt;', self.on_return)
</code></pre>

<hr>

<ol>
<li><p><a href="http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm" rel="nofollow">Events and Bindings</a></p></li>
<li><p><a href="http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm" rel="nofollow">Key names</a></p></li>
</ol>

python user-interface tkinter