Question

In Python Calculator Write a program that allows the user to enter two numbers and then...

In Python

Calculator Write a program that allows the user to enter two numbers and then adds, subtracts, divides or multiplies them when the user clicks on the appropriate button. Make sure you have a Quit button. Modification and requirements”:

_____Window title should be  your name Calculate self.main_window.title("?????")

_____Labels - change the attributes of the color, font, size and bold the answerExample self.label = tkinter.Label(self.main_window, \ text='?????????!', font=(" should Arial", 12, "bold"), fg="blue", bg="pink")Tip: choose a color from the website: colors: http://wiki.tcl.tk/37701

_____Modify Buttons to to have color

_____Put a style to your 4 math buttons and the Quit button: https://www.tutorialspoint.com/python/tk_relief.htm

_____Modify Entry labels to be right aligned (tip: justify=RIGHT)

_____Use the padx and pady to have widgets line up. Ex. self.num1_entry.pack(side='left',padx=5,pady=5) _____Add a quit button that has code to qui

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

//program to allows user to enter two numbers and then adds, subtracts, divides or multiplies them when the user clicks on the appropriate button//

//Python Code follows as for given question//

from tkinter import *

import math

class Calculator(Frame):

def __init__(self):

self.window = Tk()

self.window.title("Calculator")

# create two labels

self.l1 = Label(self.window,text="First Number")

self.l2 = Label(self.window,text="Second Number")

# to enter the text

self.e1 = Entry(self.window, justify='right')

self.e2 = Entry(self.window, justify='right')

# set the elements in a grid view

self.l1.grid(row=0,sticky=E)

self.l2.grid(row=1,sticky=E)

self.e1.grid(row=0,column=2)

self.e2.grid(row=1,column=2)

self.button_add = Button(self.window, text=" + ",font=(" should Arial", 12, "bold"), fg="blue", bg="pink", command = self.add)

self.button_sub = Button(self.window, text=" - ",font=(" should Arial", 12, "bold"), fg="blue", bg="pink", command = self.sub)

self.button_mul = Button(self.window, text=" * ",font=(" should Arial", 12, "bold"), fg="blue", bg="pink", command = self.mul)

self.button_delete = Button(self.window, text=" Quit ",font=(" should Arial", 12, "bold"), fg="blue", bg="pink", command = self.quit)

self.button_add.grid(row=5,column=1)

self.button_sub.grid(row=5,column=2)

self.button_mul.grid(row=6,column=1)

self.button_delete.grid(row=6,column=2)

self.label_result = Label(self.window,text="Result")

self.entry_result = Entry(self.window)

self.label_result.grid(row=14,sticky=E)

self.entry_result.grid(row=14,column=2)

# to run the window loop infinitely

self.window.mainloop();

def add(self):

# get the two numbers in the entry field

# convert them into float

a = float(self.e1.get())

b = float(self.e2.get())

ans = a + b

# clear the previous contents of e1 and e2

self.e1.delete(0, END)

self.e2.delete(0, END)

self.entry_result.delete(0, END)

# set the miles value in entry_result

self.entry_result.insert(0, str(ans))

def sub(self):

# get the two numbers in the entry field

# convert them into float

a = float(self.e1.get())

b = float(self.e2.get())

ans = a - b

# clear the previous contents of e1 and e2

self.e1.delete(0, END)

self.e2.delete(0, END)

self.entry_result.delete(0, END)

# set the miles value in entry_result

self.entry_result.insert(0, str(ans))

def mul(self):

# get the two numbers in the entry field

# convert them into float

a = float(self.e1.get())

b = float(self.e2.get())

ans = a * b

# clear the previous contents of e1 and e2

self.e1.delete(0, END)

self.e2.delete(0, END)

self.entry_result.delete(0, END)

# set the miles value in entry_result

self.entry_result.insert(0, str(ans))

def quit(self):

# close the app using destroy() function

self.window.destroy()

ob = Calculator()

Sample Output:

Calculator First Number Second Number Quit Result

Calculator First Number 10 Second Number Quit Result

When button is pressed Calculator First Number Second Number Quit Result 7.0

-------------------------------------------------------------------------------------------------

%%%%%%%%%%%%%%% Give Me Positive Rating Please%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
In Python Calculator Write a program that allows the user to enter two numbers and then...
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
  • in Python: Write a program that allows the user to enter a number between 1-5 and...

    in Python: Write a program that allows the user to enter a number between 1-5 and returns an animal fact based on what the user entered. The program should 1. Continue to run until the user enters “quit” to terminate the program, and 2. Validate the user’s input (the only acceptable input is 1, 2, 3, 4, 5 or “quit”). You can use the following animal facts: An octopus is a highly intelligent invertebrate and a master of disguise. Elephants...

  • USING PYTHON *Write a program, in which the user enters two numbers, then divides the first...

    USING PYTHON *Write a program, in which the user enters two numbers, then divides the first number by the second, and prints the result to two decimal places. *Make sure that that the inputs are floats (with exception handling using the ValueError). *Make sure that there is no divide by zero error (with exception handling). *Then have a plain exception: That handles any other error. -Thank you in advance for your assistance-

  • PYTHON PROGRAMMING LANGUAGE (NEEDED ASAP) Using a structured approach to writing the program: This section will...

    PYTHON PROGRAMMING LANGUAGE (NEEDED ASAP) Using a structured approach to writing the program: This section will guide you in starting the program in a methodical way. The program will display a window with GUI widgets that are associated with particular functions: Employee Payroll O X -- - - - - - - - - - - Show Payroll Find Employee by Name Highest Lowest Find Employee by Amount Write Output to Fie Cancel - -------- ------------- Notice that some of...

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
ADVERTISEMENT