Hello, it is me once more. I am using Python 2.3, and I must use Zelle's graphics class (and I must agree with many of you. Tkinter is much better). We have been tasked with creating a Graphical Scientific Calculator. In addition to the normal operators, it must contain the following buttons:
log - base 10 of display
ln - base e of display
exp - e to the display power
sin - sine of display in radians
cos - cosine of display in radians
tan - tangent of display in radians
sqrt - square root of display
1/x - reciprocal of display
X**2 - display squared
pi - append pi to display for use in successive calculations
( - append ( to display
) - append ) to display
MC - clear memory
MR - append contents of memory to display
MS - display evaluated and result copied to memory
M+ - display evaluated and result added to memory
OFF - close the calculator
I do not believe that I will have trouble calculating most of it. I am hoping someone will help me with organizing the placement of the keys in a visually pleasing manner. I am also unfamiliar with MC, MR, MS, and M+. If someone would be so kind to explain them to me, it would be appreciated. My professor simply ignored me when I asked him what exactly he meant by MC, MR, MS, M+.
The basic calculator starting code that was provided to us:
from graphics import *
from button import Button
from math import pi, e, sin, cos, tan, log, log10, exp, sqrt
class Calculator:
# This class implements a simple calculator GUI
def __init__(self):
# create the window for the calculator
win = GraphWin("calculator")
win.setCoords(0,0,9,13.5) #from (0,0,6,7)
win.setBackground("slategray")
self.win = win
# Now create the widgets
self.__createButtons()
self.__createDisplay()
def __createButtons(self):
# create list of buttons
# start with all the standard sized buttons
# bSpecs gives center coords and label of buttons
bSpecs = [(2,1,'0'), (3,1,'.'),
(1,2,'1'), (2,2,'2'), (3,2,'3'), (4,2,'+'), (5,2,'-'),
(1,3,'4'), (2,3,'5'), (3,3,'6'), (4,3,'*'), (5,3,'/'),
(1,4,'7'), (2,4,'8'), (3,4,'9'), (4,4,'<-'),(5,4,'C')]
self.buttons = []
for cx,cy,label in bSpecs:
self.buttons.append(Button(self.win,Point(cx,cy),.75,.75,label))
# create the larger = button
self.buttons.append(Button(self.win, Point(4.5,1), 1.75, .75, "="))
# activate all buttons
for b in self.buttons: b.activate()
def __createDisplay(self):
bg = Rectangle(Point(.5,12), Point(8.5,13)) #from (.5,5.5), (5.5,6.5)
bg.setFill('white')
bg.draw(self.win)
text = Text(Point(4.5,12.5), "") #from (3,6)
text.draw(self.win)
text.setFace("courier")
text.setStyle("bold")
text.setSize(12)
self.display = text
def getButton(self):
# Waits for a button to be clicked and returns the label of
# the button that was clicked.
while True:
p = self.win.getMouse()
for b in self.buttons:
if b.clicked(p):
return b.getLabel() # method exit
def processButton(self, key):
# Updates the display of the calculator for press of this key
text = self.display.getText()
if key == 'C':
self.display.setText("")
elif key == '<-':
# Backspace, slice off the last character.
self.display.setText(text[:-1])
elif key == '=':
# Evaluate the expresssion and display the result.
# the try...except mechanism "catches" errors in the
# formula being evaluated.
try:
result = eval(text)
except: result = 'ERROR'
self.display.setText(str(result))
else:
# Normal key press, append it to the end of the display
self.display.setText(text+key)
def run(self):
# Infinite 'event loop' to process button clicks.
while True:
key = self.getButton()
self.processButton(key)
# This runs the program.
if __name__ == '__main__':
# First create a calculator object
theCalc = Calculator()
# Now call the calculator's run method.
theCalc.run()
Any suggestions would be appreciated.