questions about Exceptions?

skanemupp at yahoo.se skanemupp at yahoo.se
Thu Apr 10 08:11:53 EDT 2008


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)


x = 0.1
y = 0.4
for char in '123+456-789*0^./()':
    b = Button(mygui, text=char,command=lambda n=char:Disp(n),
width=2, height=1)
    b.place(relx=x, rely=y, anchor=CENTER)
    x=x+0.1
    if x==0.5:
        x=0.1
        y=y+0.1

b = Button(mygui, text="C",command=Erase, 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