d. Fix a bug in the current code. In current code, after you complete an operation, the new operation won't generate correct result. For example, if you perform operation "121 + 34 =" by pressing the following keypads 2 1 3 || 4 the display field will show 155.0, which is correct. 155 However, if you continue to press It doesn't perform the operation “155 – 34 =". Instead, the display will show “-34=" which will not generate correct result. Please fix that bug!

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I can't figure out the error in this code. Heres the code: 

from tkinter import *

class Calculator:
# define the text on each button
ButtonValues=[['7','8','9','/'], ['4','5','6','*'], ['1','2','3','-'], ['0','C','=','+']]
def __init__(self):
self.Calc = Tk()
self.Calc.title('Calculator')

# Create two frames: frame1 is to house the display acreen
# frame2 is to house all the buttons
self.frame1=Frame(self.Calc)
self.frame2=Frame(self.Calc)

#
self.expression = ""
self.equation=StringVar()

# Create an Entry display and bind the entry to a StringVar instance
# equation:
# You can call set() to set a value in the entry or call get() to
# get a value from the entry
self.theDisplay = Entry(self.frame1, width=30, textvariable=self.equation)
self.myButtons= []

# Define all the buttons
def SetButtons(self):
self.frame1.pack()
self.frame2.pack(side=BOTTOM)

self.theDisplay.grid(row=0)

self.myButtons.append([])
self.myButtons[0].append(Button(self.frame2,text=Calculator.ButtonValues[0][0], font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[0][0])))
self.myButtons[0].append(Button(self.frame2,text=Calculator.ButtonValues[0][1],font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[0][1])))
self.myButtons[0].append(Button(self.frame2,text=Calculator.ButtonValues[0][2],font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[0][2])))
self.myButtons[0].append(Button(self.frame2,text=Calculator.ButtonValues[0][3],font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[0][3])))
self.myButtons.append([])
self.myButtons[1].append(Button(self.frame2,text=Calculator.ButtonValues[1][0], font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[1][0])))
self.myButtons[1].append(Button(self.frame2,text=Calculator.ButtonValues[1][1],font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[1][1])))
self.myButtons[1].append(Button(self.frame2,text=Calculator.ButtonValues[1][2],font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[1][2])))
self.myButtons[1].append(Button(self.frame2,text=Calculator.ButtonValues[1][3],font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[1][3])))
self.myButtons.append([])
self.myButtons[2].append(Button(self.frame2,text=Calculator.ButtonValues[2][0], font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[2][0])))
self.myButtons[2].append(Button(self.frame2,text=Calculator.ButtonValues[2][1],font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[2][1])))
self.myButtons[2].append(Button(self.frame2,text=Calculator.ButtonValues[2][2],font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[2][2])))
self.myButtons[2].append(Button(self.frame2,text=Calculator.ButtonValues[2][3],font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[2][3])))
self.myButtons.append([])
self.myButtons[3].append(Button(self.frame2,text=Calculator.ButtonValues[3][0], font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[3][0])))
self.myButtons[3].append(Button(self.frame2,text=Calculator.ButtonValues[3][1],font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[3][1])))
self.myButtons[3].append(Button(self.frame2,text=Calculator.ButtonValues[3][2],font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[3][2])))
self.myButtons[3].append(Button(self.frame2,text=Calculator.ButtonValues[3][3],font=8,width=4,
command=lambda:self.Press(Calculator.ButtonValues[3][3])))

# position the buttons in frame2
for i in range(4):
for j in range(4):
self.myButtons[i][j].grid(row=i, column=j, sticky="nsew")
  
# This method is triggered by pressing a button. It will do the following
# (1) update expression if the button is not "=" or "C"
# (2) if the button "=" is pressed, call eval() to evaluate the
# expression and display the result by calling set() in StringVar
# instance equation
# (3) if the button "C" is pressed, set expression to "" and set display
# to ""
#
def Press(self, num):
   # If the button "=" is pressed, then the expression will be
   # evaluated
if num == '=':
total = str(eval(self.expression))
self.equation.set(total)
self.expression = ""
elif num == 'C':
self.expression = ""
self.equation.set("")
else:
# point out the global expression variable
# global self.expression
# concatenation of string
self.expression = self.expression + num
# update the expression by using set method
self.equation.set(self.expression)   
  
  
# main program

myCalc = Calculator()
myCalc.SetButtons()
myCalc.Calc.mainloop()

d. Fix a bug in the current code. In current code, after you complete an operation,
the new operation won't generate correct result. For example, if you perform
operation "121 + 34 =" by pressing the following keypads
2 1
3 || 4
the display field will show 155.0, which is correct.
155
However, if you continue to press
It doesn't perform the operation “155 – 34 =". Instead, the display will show
“-34=" which will not generate correct result.
Please fix that bug!
Transcribed Image Text:d. Fix a bug in the current code. In current code, after you complete an operation, the new operation won't generate correct result. For example, if you perform operation "121 + 34 =" by pressing the following keypads 2 1 3 || 4 the display field will show 155.0, which is correct. 155 However, if you continue to press It doesn't perform the operation “155 – 34 =". Instead, the display will show “-34=" which will not generate correct result. Please fix that bug!
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 6 images

Blurred answer
Knowledge Booster
Passing Array as Argument
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education