Question

(code in Python using import tkinter) Write a GUI program to develop a simple math calculator....

(code in Python using import tkinter) Write a GUI program to develop a simple math calculator. The user should be able to enter two numbers, and click one of four buttons to do the +, -, *, or / operations. Then the program will display the result. Note: if the divisor in the / operation is 0, display ‘N/A’ in the result.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

from tkinter import *


class MathCalculator:

def __init__(self):

window = Tk()

window.title("Math Calculator")

Label(window, text="Number #1").grid(row=1, column=1, sticky=W)

Label(window, text="Number #2").grid(row=2, column=1, sticky=W)

Label(window, text="Result").grid(row=3, column=1, sticky=W)

self.num1 = StringVar()

Entry(window, textvariable=self.num1,

justify=RIGHT).grid(row=1, column=5)

self.num2 = StringVar()

Entry(window, textvariable=self.num2,

justify=RIGHT).grid(row=2, column=5)

self.result = StringVar()

Entry(window, textvariable=self.result,

justify=RIGHT).grid(row=3, column=5)

btAdd = Button(window, text="+",

command=self.computeAdd).grid(row=4, column=1, sticky=E)

btSub = Button(window, text="-",

command=self.computeSub).grid(row=4, column=2, sticky=E)

btDiv = Button(window, text="/",

command=self.computeDiv).grid(row=4, column=3, sticky=E)

btMul = Button(window, text="*",

command=self.computeMul).grid(row=4, column=4, sticky=E)

window.mainloop()

def computeAdd(self):

output = int(self.num1.get()) + int(self.num2.get())

self.result.set(format(output, ".2f"))

def computeSub(self):

output = int(self.num1.get()) - int(self.num2.get())

self.result.set(format(output, ".2f"))

def computeDiv(self):

if(int(self.num2.get()) == 0):

self.result.set("N/A")

else:

output = int(self.num1.get()) / int(self.num2.get())

self.result.set(format(output, ".2f"))

def computeMul(self):

output = int(self.num1.get()) * int(self.num2.get())

self.result.set(format(output, ".2f"))


def main():

MathCalculator()


main()

Output:

Add a comment
Know the answer?
Add Answer to:
(code in Python using import tkinter) Write a GUI program to develop a simple math calculator....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT