Add functionality—addition, subtraction, multiplication and division—to the calculator cre- ated in Exercise 10.3. Use the built-in Python function eval to evaluates strings. For instance, eval( "34+24" ) returns the integer 58.

What will be an ideal response?

```
# Using Grid to create a functional calculator GUI

from Tkinter import *

class Calculator( Frame ):
"""Create a functional calculator GUI"""

expression = "" # mathematical expression

def __init__( self ):
"""Create calculator GUI"""

Frame.__init__( self )
self.grid( sticky = W+E+N+S )
self.master.title( "Calculator" )
self.master.rowconfigure( 0, weight = 1 )
self.master.columnconfigure( 0, weight = 1 )

# the Entry widget fits the entire cell
self.display = Entry( self )
self.display.grid( row = 0, columnspan = 4,
sticky = W+E )

# create and insert number buttons
self.numberButtons = []

for i in range( 9, -1, -1 ):

self.numberButton = Button( self, text = str( i ),
width = 5, name = str( i ) )
self.numberButton.bind( "",
self.pressedNumber )
self.numberButtons.append( self.numberButton )

current = 0

for x in range( 1, 4 ):

for y in range( 2, -1, -1 ):

self.numberButtons[ current ].grid( row = x,
column = y )
current += 1

self.numberButtons[ -1 ].grid( row = 4, column = 0 )

# insert decimal button
self.decimalButton = Button( self, text = ".", width = 5,
name = "decimal" )
self.decimalButton.bind( "", self.decimal )
self.decimalButton.grid( row = 4 , column = 1 )

# insert operator buttons
self.operators = [ "/", "*", "-", "+" ]
x = 1

for operator in self.operators:

self.operatorButton = Button( self, text = operator,
width = 5, name = operator )
self.operatorButton.bind( "",
self.operator )
self.operatorButton.grid( row = x, column = 3 )
x += 1

self.equalsButton = Button( self, text = "=", width = 5,
name = "=" )
self.equalsButton.bind( "", self.calculate )
self.equalsButton.grid( row = 4, column = 2 )

# pressedNumber handles the number buttons
def pressedNumber( self, event ):
"""Event handler for numerical buttons"""

# delete a previous answer (if any)
if self.expression == "":
self.display.delete( 0, END )

number = self.currentButton( event )

# add number to the expression
self.expression += number

# add number to the display on the calculator
self.display.insert( INSERT, number )

# insert a decimal point
def decimal( self, event ):
"""Event handler for decimal button"""

self.expression += "."
self.display.insert( INSERT, "." )

# each operator clears the display and is
# added to the expression string
def operator( self, event ):
"""Event handler for operator buttons"""

operator = self.currentButton( event )

# subtraction sign could signify a negative number
if operator == "-":

# negative number - display sign
if self.expression == "" or \
self.expression[ -1 ] in self.operators:

if self.expression == "":
self.display.delete( 0, END )

self.expression += operator
self.display.insert( INSERT, "-" )
return

self.expression += operator
self.display.delete( 0, END )

# expression is evaluated and displayed
def calculate( self, event ):
"""Return calculation"""

self.display.delete( 0, END )

# use eval() to evaluate the expression
answer = float( eval ( self.expression ) )
self.display.insert( INSERT, answer )
self.expression = "" # expression is cleared

def currentButton( self, event ):
"Returns number of button associated with event"

button = ( str( event.widget ) ).split( "." )

return button[ 2 ]

def main():
Calculator().mainloop()

if __name__ == "__main__":
main()
```
![15064|287x224](upload://6msHwlVF1NJYiFL40VKUCDx66Bv.png)

Computer Science & Information Technology

You might also like to view...

Text files are imported into Excel in a similar manner as XML and HTML files

Indicate whether the statement is true or false.

Computer Science & Information Technology

Four bits is called a _________.

A. radix point B. byte C. nibble D. binary digit

Computer Science & Information Technology