questions about Exceptions?

skanemupp at yahoo.se skanemupp at yahoo.se
Thu Apr 10 13:41:06 CEST 2008


On 10 Apr, 13:15, cokofree... at gmail.com wrote:
> > the erase() id alwys need if u wanna abort whilst u wrote something.
>
> 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':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.4, anchor=CENTER)
b = Button(mygui, text="2",command=lambda n='2':Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.4, anchor=CENTER)
b = Button(mygui, text="3",command=lambda n='3':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.4, anchor=CENTER)
b = Button(mygui, text="+",command=lambda n='+':Disp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.4, anchor=CENTER)
b = Button(mygui, text="4",command=lambda n='4':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.5, anchor=CENTER)
b = Button(mygui, text="5",command=lambda n='5':Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.5, anchor=CENTER)
b = Button(mygui, text="6",command=lambda n='6':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.5, anchor=CENTER)
b = Button(mygui, text="-",command=lambda n='-':Disp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.5, anchor=CENTER)
b = Button(mygui, text="7",command=lambda n='7':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.6, anchor=CENTER)
b = Button(mygui, text="8",command=lambda n='8':Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.6, anchor=CENTER)
b = Button(mygui, text="9",command=lambda n='9':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.6, anchor=CENTER)
b = Button(mygui, text="*",command=lambda n='*':Disp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.6, anchor=CENTER)
b = Button(mygui, text="0",command=lambda n='0':Disp(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='**':Disp(n), width=2,
height=1)
b.place(relx=0.3, rely=0.7, anchor=CENTER)
b = Button(mygui, text="/",command=lambda n='/':Disp(n), width=2,
height=1)
b.place(relx=0.4, rely=0.7, anchor=CENTER)
b = Button(mygui, text=".",command=lambda n='.':Disp(n), width=2,
height=1)
b.place(relx=0.1, rely=0.8, anchor=CENTER)
b = Button(mygui, text="(",command=lambda n='(':Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.8, anchor=CENTER)
b = Button(mygui, text=")",command=lambda n=')':Disp(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()



More information about the Python-list mailing list