Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python Project

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Project

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import tkinter as tk

import math
import cmath

# Function for basic operations


def add(x, y):
return x + y

def subtract(x, y):


return x - y

def multiply(x, y):


return x * y

def divide(x, y):


if y == 0:
return "Error! Division by zero."
else:
return x / y

# Function for scientific operations


def square_root(x):
try:
return math.sqrt(x)
except ValueError:
return "Error! Negative input for square root."

def power(x, y):


return math.pow(x, y)

def logarithm(x, base=10):


if x <= 0 or base <= 0:
return "Error! Logarithm of non-positive numbers is undefined."
else:
return math.log(x, base)

def sine(x):
return math.sin(math.radians(x))

def cosine(x):
return math.cos(math.radians(x))

def tangent(x):
return math.tan(math.radians(x))

def factorial(x):
if x < 0:
return "Error! Factorial is undefined for negative numbers."
else:
return math.factorial(int(x))

# Handle complex number operations


def complex_add(x, y):
return x + y

def complex_multiply(x, y):


return x * y

def complex_conjugate(x):
return x.conjugate()

# Clear the input field


def clear():
display.set("")

# Calculate the result based on the user input


def calculate():
try:
expression = entry.get()
result = eval(expression, {"__builtins__": None}, {
"sin": math.sin,
"cos": math.cos,
"tan": math.tan,
"log": math.log,
"sqrt": math.sqrt,
"pow": math.pow,
"factorial": math.factorial,
"abs": abs,
"pi": math.pi,
"e": math.e,
"exp": math.exp,
"complex": complex,
"pi": math.pi,
})
display.set(result)
except Exception as e:
display.set(f"Error: {str(e)}")

# Switch between complex number and real input mode


def toggle_complex():
current = complex_mode.get()
complex_mode.set(not current)

# Create the main window


window = tk.Tk()
window.title("Scientific Calculator")
window.geometry("400x600")

# StringVar for the input field


display = tk.StringVar()

# Entry widget for displaying the expression and result


entry = tk.Entry(window, textvar=display, font=("Arial", 14), bd=10,
relief="solid", justify="right")
entry.grid(row=0, column=0, columnspan=4)

# Button functions
button_texts = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+',
'sin', 'cos', 'tan', 'sqrt',
'log', 'pow', 'abs', 'exp',
'pi', 'e', 'factorial', '(',
')', '.', 'complex', 'clear'
]
buttons = []

# Add buttons dynamically based on button_texts


for i, text in enumerate(button_texts):
if text == "=":
button = tk.Button(window, text=text, width=10, height=3, font=("Arial",
14),
command=calculate)
elif text == "clear":
button = tk.Button(window, text="Clear", width=10, height=3, font=("Arial",
14), command=clear)
else:
button = tk.Button(window, text=text, width=10, height=3, font=("Arial",
14),
command=lambda t=text: entry.insert(tk.END, t) if t !=
"=" else None)
row = (i // 4) + 1
col = i % 4
button.grid(row=row, column=col, padx=5, pady=5)

# Special button for clear


clear_button = tk.Button(window, text="C", width=10, height=3, font=("Arial", 14),
command=clear)
clear_button.grid(row=5, column=0, padx=5, pady=5)

# Toggle button for switching complex mode


complex_mode = tk.BooleanVar(value=False)
toggle_button = tk.Checkbutton(window, text="Complex Mode", variable=complex_mode,
font=("Arial", 12))
toggle_button.grid(row=5, column=1, padx=5, pady=5)

# Run the application


window.mainloop()

You might also like