Question: Create a python code for a calculator to Create four text boxes. Read the A , B , C and D values from these from

Create a python code for a calculator to Create four text boxes. Read the A, B, C and D values from these from tkinter import *
#Importing the Math library
import mathexpression =""in the text entry box
def press(num):global expressionexpression = expression + str(num)equation.set(expression)def equalpress():for handling the errors like zeroPut that code inside the try blocktry:
global expression*,/,total = eval("import('math')")
total = eval(expression)equation.set(total)
#setting expression value to the string of total so it can be used
further
expression = str(total)by the except block
except:equation.set(" Error ")expression ="" def clear():
global expressionexpression =""equation.set("")#The following function "def sqrt()" isn't used and is not needed in this version -
It was used in the old versiondef sqrt():global expressionmust convert to interger to use in equation
expression = math.sqrt(int(expression))truncate decimals
equation.set(int(expression))used further
expression = str(expression)Program code
if =="":gui = Tk()gui.configure(background="light blue")gui.title("Calculator")gui.geometry("362x125")we create an instance of this class
equation = StringVar()expression_field = Entry(gui, textvariable=equation)certain number of columns & rows # sets the size of the text box, span across columns & span length
expression_field.grid(columnspan=5, ipadx=120)
# sets the initial text of the text box
equation.set('Enter Your Expression')
####### names ={'sqrt': sqrt, 'power': pow}
# create a Button, with text in it, font color, background color,
# Uses Lambda commands for each number or expression OR commands for a
defined function above
# Height & Length for the size of the button
# Then places the Button on the grid in a certain row & column: Rows start at
2, and Columns at 0
button1= Button(gui, text='1', fg='black', bg='pink', command=lambda:
press(1), height=1, width=7)
button1.grid(row=4, column=0)
button2= Button(gui, text='2', fg='black', bg='pink', command=lambda:
press(2), height=1, width=7)
button2.grid(row=4, column=1)
button3= Button(gui, text='3', fg='black', bg='pink', command=lambda:
press(3), height=1, width=7)
button3.grid(row=4, column=2)
button4= Button(gui, text='4', fg='black', bg='pink', command=lambda:
press(4), height=1, width=7)
button4.grid(row=3, column=0)
button5= Button(gui, text='5', fg='black', bg='pink', command=lambda:
press(5), height=1, width=7)
button5.grid(row=3, column=1)
button6= Button(gui, text='6', fg='black', bg='pink', command=lambda:
press(6), height=1, width=7)
button6.grid(row=3, column=2)
button7= Button(gui, text='7', fg='black', bg='pink', command=lambda:
press(7), height=1, width=7)
button7.grid(row=2, column=0)
button8= Button(gui, text='8', fg='black', bg='pink', command=lambda:
press(8), height=1, width=7)
button8.grid(row=2, column=1)
button9= Button(gui, text='9', fg='black', bg='pink', command=lambda:
press(9), height=1, width=7)
button9.grid(row=2, column=2)
button 0= Button(gui, text='0', fg='black', bg='pink', command=lambda:
press(0), height=1, width=7)
button0.grid(row=5, column=1)
plus = Button(gui, text='+', fg='black', bg='yellow', command=lambda:
press("+"), height=1, width=7)
plus.grid(row=2, column=3)
minus = Button(gui, text='-', fg='black', bg='yellow', command=lambda: press("-"), height=1, width=7)
minus.grid(row=3, column=3)
multiply = Button(gui, text='*', fg='black', bg='yellow', command=lambda:
press("*"), height=1, width=7)
multiply.grid(row=4, column=3)
divide = Button(gui, text='/', fg='black', bg='yellow', command=lambda:
press("/"), height=1, width=7)
divide.grid(row=5, column=3)
equal = Button(gui, text='=', fg='black', bg='light green',
command=equalpress, height=1, width=7)
equal.grid(row=5, column=2)
clear = Button(gui, text='Clear', fg='black', bg='red', command=clear,
height=1, width=7)
clear.grid(row=2, column=4)
sqrt = Button(gui, text=' V ', fg='black', bg='white', command=lambda:
press("math.sqrt("), height=1, width=7)
sqrt.grid(row=3, column=4)
dec = Button(gui, text=',', fg='black', bg='orange', command=lambda:
press("."), height=1, width=7)
dec.grid(row=5, column=0)
paranthesis = Button(gui, text=')', fg='black', bg='white', command=lambda:
press(")"), height=1, width=7)
paranthesis.grid(row=4, column=4)Starts the GUI
gui.mainloop()
text boxes. Calculate the addition of those numbers:
((A + B )*2+(C + D )*3)
Where A, B and C are the inputs to be read from the text boxes.
Create a python code for a calculator to Create

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!