Calculator – Python Source Code

  • Code
  • Code With GUI
# Function to calculate
def calculator():
    try:
        # Inform the user to enter an equation.
        user_equation = input("Enter Your Equation : ")

        # Calculate the equation using the eval() function
        equation_result = eval(user_equation)

        # Display the result of the equation
        print("Answer :", equation_result)

    except Exception as e:
        # Handle any exceptions that arise during evaluation
        print("Error:", e)

# Create an infinite loop to keep the calculator running
while True:
    # Call the calculator function to perform calculations
    calculator()

    # Ask the user if they want to perform another calculation
    user_input = input("Do you want to perform Other calculation? (yes/no): ")

    # Check if the user wants to exit the calculator
    if user_input.lower() != "yes":

        # Display a thank you message and break the loop
        print("Thanks For Using Our Calculator")
        break

# Calculator Source Code - By Codotix
# Importing required module
import tkinter as tk

# Function to handle button clicks
def Button_Click(object):
    # Get the current content of the display
    Current_Screen = entry_var.get()
    # Get the text of the clicked button
    screen_text = object.widget.cget("text")
    
    # If the "=" button is clicked
    if screen_text == "=":
        try:
            # Evaluate the expression and update the display
            result = eval(Current_Screen)
            entry_var.set(result)

        except Exception as e:
            # Display "Error" in case of an evaluation error
            entry_var.set("Error")

    # If the "C " button is clicked, clear the display
    elif screen_text == "C ":
        entry_var.set("")

    # For other buttons, update the display with the button's text
    else:
        entry_var.set(Current_Screen + screen_text)

# Create the main window
root = tk.Tk()
root.minsize(250, 300)
root.geometry("400x500")
root.title("Calculator")

# Create a StringVar to hold the content of the display
entry_var = tk.StringVar()

# Create an entry field for displaying and entering calculations
User_Entry = tk.Entry(root,textvariable=entry_var, background="#FFEFC8",font=("arial 40 bold"), border=4, relief="ridge", justify="right")
User_Entry.pack(ipadx=10, ipady=10, padx=10, pady=10, fill="both", expand=True)

# Create a frame to hold the calculator buttons
Button_Frame = tk.Frame(root)
Button_Frame.pack(fill="both", expand=True)

# Define the layout of the buttons
Buttons = [
    ("7", "8", "9", "/"),
    ("4", "5", "6", "*"),
    ("1", "2", "3", "-"),
    (". ", "0", "(", ")"),
    ("C ", "=")   
]

# Create buttons and arrange them in the layout
for row in Buttons:
    Button_Row = tk.Frame(Button_Frame)
    Button_Row.pack(fill="both", expand=True)
    for label in row:
        button = tk.Button(Button_Row, text=label, background="#FFE099",font="Arial  16 bold", bd=3, relief="ridge")
        button.pack(side="left", fill="both", expand=True)
        button.bind("<Button-1>", Button_Click)

# Start the GUI event loop
root.mainloop()

# Calculator Source Code - By Codotix

Leave a Comment

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