[Tutor] A Challenge solution
Jesse W
jessw@loop.com
Sun, 24 Jun 2001 15:04:51 -0700
--Message-Boundary-20185
Content-type: text/plain; charset=US-ASCII
Content-transfer-encoding: 7BIT
Content-description: Mail message body
Oh Honorable Lord of Python(s),
Enclosed is my solution to the calculator challenge on Useless
Python. Please let me know when my certified 14ft. long Useless
Python(Pythonicus uslessium) will be arriving. ;-)
Thank you, and happy hacking,
Jesse Weinstein
And to all you of at tutor:
Any comments, suggestions, patches, gripes, or simple
screams of emotion are welcomed. Please only post to the list.
(Oh, so you want an explanation of why this message is apperently
only going to the list, not to anyone at Useless Python? Well, I sent
it once before, but I put tutor's address in wrong. So I am resending
it to you guys, but I don't have to resend it to the people at Useless
Python. Plus, I think most of the people at Useless are also on the
list. :-) Happy?)
--Message-Boundary-20185
Content-type: text/plain; charset=US-ASCII
Content-transfer-encoding: 7BIT
Content-description: Text from file 'calculator.py'
#Simple calculator app with a GUI front end, as per the "Python Challenge".
#Written in __exactly__ 1 hour.
#By Jesse Weinstein-jessw@loop.com
#Released on June 24, 2001, at 2:42 PM, Pacific Standard Time
from Tkinter import *
class Calculator:
def __init__(self, font_size=12):
self.font_size=font_size
self.make_window()
def make_window(self):
self.root=Tk()
self.root.title('Calculator')
self.opersF=Frame(self.root)
self.opers=[]
for item in ['+', '-', '*', '/']:
self.opers.append(Button(self.opersF, text=item[0],\
height=1, width=2,\
font=('', `self.font_size`, '')))
self.opers[-1].bind('<1>', self.buttonCB)
self.opers[-1].pack(side=TOP)
self.display=Entry(self.root, font=('', `self.font_size`, ''),\
state=DISABLED, width=2)
self.numsF=Frame(self.root)
self.nums=[]
for item in range(1, 10):
self.nums.append(Button(self.numsF, text=`item`,\
height=1, width=2, \
font=('', `self.font_size`, '')))
self.nums[-1].bind('<1>', self.buttonCB)
self.nums[-1].grid(column=(item-1) % 3, row=(item-1)/3)
self.othersF=Frame(self.root)
self.others=[]
for item in [('=', self.equals), ('C', self.clear),\
('d', self.backspace)]:
self.others.append(Button(self.othersF, text=item[0],\
command=item[1], height=1, width=2,
font=('', `self.font_size`, '')))
self.others[-1].pack(side=RIGHT)
self.display.grid(column=0, row=0, columnspan=4, sticky=NW+E)
self.opersF.grid(column=3, row=1, rowspan=2, sticky=W)
self.numsF.grid(column=0, row=1, columnspan=3, sticky=SE)
self.othersF.grid(column=2, row=2, sticky=NE)
def buttonCB(self, event):
val=event.widget.cget('text')
self.display.config(state=NORMAL)
self.display.insert(END, val)
self.display.config(state=DISABLED)
def equals(self):
if self.display.get():
ans=eval(self.display.get())
self.display.config(state=NORMAL)
self.display.delete(0, END)
self.display.insert(0, ans)
self.display.config(state=DISABLED)
def clear(self):
self.display.config(state=NORMAL)
self.display.delete(0, END)
self.display.config(state=DISABLED)
def backspace(self):
self.display.config(state=NORMAL)
self.display.delete(len(self.display.get())-1)
self.display.config(state=DISABLED)
if __name__=='__main__':
it=Calculator()
print """If you are seeing this message because nothing else has appeared
on your screen, then I suggest you run this from IDLE, or uncomment out the line
directly below this one, which starts a Tkinter mainloop. (If you start a
mainloop in IDLE, it locks up.)"""
#it.root.mainloop()
#Note: the above should be uncommented if running outside of IDLE
--Message-Boundary-20185--