raceback (most recent call last):
File "C:/Python32/n", line 1, in <module>
from Tkinter import *
# button text list
cmdlst = ['7', '8', '9', '+', '%',
'4', '5', '6', '-', '**',
'1', '2', '3', '*', '//',
'.', '0', 'CL', '/', '=']
class MyButton(Button):
backref = None
def Click(self):
# back reference
self.backref.BtnCmd(self["text"])
class CalcApp:
def __init__(self, master):
frame = Frame(master)
self.textbox = Entry(width=30, takefocus=1)
self.textbox.pack(side=TOP)
self.textbox.focus_force()
self.buttons = []
for n, c in enumerate(cmdlst):
self.buttons.append(MyButton(frame, text=c, width=5))
self.buttons[n]["command"] = self.buttons[n].Click
self.buttons[n].backref = self
self.buttons[n].grid(row=n/5, column=n%5)
frame.pack()
def BtnCmd(self, cmd):
if cmd == '=':
try:
res = eval(self.textbox.get())
except:
res = "Error!"
self.textbox.delete(0, END)
self.textbox.insert(0, str(res))
elif cmd == 'CL':
self.textbox.delete(0, END)
else:
self.textbox.insert(END, cmd)
root = Tk()
root.title("EWCalc")
calcapp = CalcApp(root)
root.mainloop()