Python Project – Calculator

Python Project - Calculator

A calculator is an electronic device that performs mathematical operations such as addition, subtraction, multiplication, and division. It can also perform more advanced operations such as trigonometric functions, logarithms, and exponential functions.

There are also different types of calculators, such as scientific calculators, graphing calculators, and basic calculators. Nowadays, calculators can also be found as software applications on computers, smartphones, and other digital devices.

Python Project - Calculator python code

from tkinter import *

# define functions for each operation
def add():
    result.set(float(num1.get()) + float(num2.get()))

def subtract():
    result.set(float(num1.get()) – float(num2.get()))

def multiply():
    result.set(float(num1.get()) * float(num2.get()))

def divide():
    if float(num2.get()) == 0:
        result.set(“Cannot divide by zero”)
    else:
        result.set(float(num1.get()) / float(num2.get()))

# create a Tkinter window
window = Tk()
window.title(“Calculator”)

# create labels and entry fields for input and output
Label(window, text=”Enter first number:”).grid(row=0, column=0)
num1 = Entry(window)
num1.grid(row=0, column=1)

Label(window, text=”Enter second number:”).grid(row=1, column=0)
num2 = Entry(window)
num2.grid(row=1, column=1)

Label(window, text=”Result:”).grid(row=2, column=0)
result = StringVar()
output = Label(window, textvariable=result)
output.grid(row=2, column=1)

# create buttons for each operation
Button(window, text=”Add”, command=add).grid(row=3, column=0)
Button(window, text=”Subtract”, command=subtract).grid(row=3, column=1)
Button(window, text=”Multiply”, command=multiply).grid(row=4, column=0)
Button(window, text=”Divide”, command=divide).grid(row=4, column=1)

# start the main event loop
window.mainloop()

Leave a Reply

Your email address will not be published. Required fields are marked *