From 2349374dff07699dba8abc2816730f81e9e4151e Mon Sep 17 00:00:00 2001 From: Archana Saroj Date: Fri, 24 Nov 2023 23:08:13 +0530 Subject: [PATCH] This code implements a graphical calculator in Python using Tkinter, allowing users to perform arithmetic operations and supported mathematical functions, including sine (sin), cosine (cos), tangent (tan), logarithm (log), and natural logarithm (ln), with error handling for invalid input. --- apps/Full_Calulator_GUI.py | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 apps/Full_Calulator_GUI.py diff --git a/apps/Full_Calulator_GUI.py b/apps/Full_Calulator_GUI.py new file mode 100644 index 0000000..f219a03 --- /dev/null +++ b/apps/Full_Calulator_GUI.py @@ -0,0 +1,63 @@ +import tkinter as tk +import math + +def evaluate_expression(): + try: + expression = entry.get() + # Replace trigonometric and logarithmic functions in the expression + expression = expression.replace("sin", "math.sin") + expression = expression.replace("cos", "math.cos") + expression = expression.replace("tan", "math.tan") + expression = expression.replace("log", "math.log10") + expression = expression.replace("ln", "math.log") + + result = str(eval(expression)) + entry.delete(0, tk.END) + entry.insert(tk.END, result) + except Exception as e: + entry.delete(0, tk.END) + entry.insert(tk.END, "Error") + +def clear_entry(): + entry.delete(0, tk.END) + +def button_click(char): + current_text = entry.get() + entry.delete(0, tk.END) + entry.insert(tk.END, current_text + char) + +# Create the main window +root = tk.Tk() +root.title("Calculator") + +# Create an entry widget for input and output +entry = tk.Entry(root, width=30) +entry.grid(row=0, column=0, columnspan=4) + +# Create buttons for digits and operators +buttons = [ + '7', '8', '9', '/', + '4', '5', '6', '*', + '1', '2', '3', '-', + '0', '.', '+', + 'sin', 'cos', 'tan', + 'log', 'ln', '(', ')', + # Moved the "=" button to the end +] + +row_val = 1 +col_val = 0 + +for button in buttons: + tk.Button(root, text=button, width=5, height=2, command=lambda b=button: button_click(b)).grid(row=row_val, column=col_val) + col_val += 1 + if col_val > 3: + col_val = 0 + row_val += 1 + +# Create clear and evaluate buttons +tk.Button(root, text="C", width=5, height=2, command=clear_entry).grid(row=5, column=0) +# The "=" button is now at the end of the buttons list +tk.Button(root, text="=", width=5, height=2, command=evaluate_expression).grid(row=5, column=3) + +root.mainloop()