But if it is meant to only evaluate once you've pressed the enter key
(I take it?) you shouldn't need that. And if you are to abort while
evaluating it will not do that.
CHANGED IT NOW TO A MUCH BETTER SOLUTION(will see if i can make a
better solution with the buttons, hate big blocks of similarlooking
code):
from __future__ import division
import Tkinter
from Tkinter import *
mygui = Tkinter.Tk()
mygui.title("Calculator")
l = Label(mygui, text="Answer: ")
l.place(relx=0.15, rely=0.2, anchor=CENTER)
e = Entry(mygui)
e.place(relx=0.4, rely=0.1, anchor=CENTER)
def Disp(nstr):
e.insert(END, nstr)
def Calc():
expr=e.get()
try:
b = Label(mygui, text=eval(expr))
b.place(relx=0.4, rely=0.2, anchor=CENTER)
except:
b = Label(mygui, text="Not computable")
b.place(relx=0.4, rely=0.2, anchor=CENTER)
def Erase():
e.delete(0,END)
b = Button(mygui, text="1",command=lambda n='1'
isp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.4, anchor=CENTER)
b = Button(mygui, text="2",command=lambda n='2'
isp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.4, anchor=CENTER)
b = Button(mygui, text="3",command=lambda n='3'
isp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.4, anchor=CENTER)
b = Button(mygui, text="+",command=lambda n='+'
isp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.4, anchor=CENTER)
b = Button(mygui, text="4",command=lambda n='4'
isp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.5, anchor=CENTER)
b = Button(mygui, text="5",command=lambda n='5'
isp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.5, anchor=CENTER)
b = Button(mygui, text="6",command=lambda n='6'
isp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.5, anchor=CENTER)
b = Button(mygui, text="-",command=lambda n='-'
isp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.5, anchor=CENTER)
b = Button(mygui, text="7",command=lambda n='7'
isp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.6, anchor=CENTER)
b = Button(mygui, text="8",command=lambda n='8'
isp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.6, anchor=CENTER)
b = Button(mygui, text="9",command=lambda n='9'
isp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.6, anchor=CENTER)
b = Button(mygui, text="*",command=lambda n='*'
isp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.6, anchor=CENTER)
b = Button(mygui, text="0",command=lambda n='0'
isp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.7, anchor=CENTER)
b = Button(mygui, text="C",command=Erase, width=2, height=1)
b.place(relx=0.2, rely=0.7, anchor=CENTER)
b = Button(mygui, text="^",command=lambda n='**'
isp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.7, anchor=CENTER)
b = Button(mygui, text="/",command=lambda n='/'
isp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.7, anchor=CENTER)
b = Button(mygui, text=".",command=lambda n='.'
isp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.8, anchor=CENTER)
b = Button(mygui, text="(",command=lambda n='('
isp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.8, anchor=CENTER)
b = Button(mygui, text=")",command=lambda n=')'
isp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.8, anchor=CENTER)
b = Button(mygui, text="=",command=Calc, width=2, height=1)
b.place(relx=0.4, rely=0.8, anchor=CENTER)
mygui.mainloop()