From 7af1e9ea043acb34b3653b5637c9a11dcdc50d5a Mon Sep 17 00:00:00 2001 From: Akshat Chauhan Date: Wed, 29 Oct 2025 16:12:05 +0530 Subject: [PATCH 1/2] added GUI Calculator --- tinker_calculator/calculator.py | 104 ++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 tinker_calculator/calculator.py diff --git a/tinker_calculator/calculator.py b/tinker_calculator/calculator.py new file mode 100644 index 00000000..2adf1d15 --- /dev/null +++ b/tinker_calculator/calculator.py @@ -0,0 +1,104 @@ +import tkinter as tk +from tkinter import messagebox + +def click_button(value): + """Adds the clicked value (number or operator) to the entry field.""" + current = entry.get() + + if not current and value in '+-*/.': + return + if current and current[-1] in '+-*/.' and value in '+-*/.': + if value in '+-*/': + entry.delete(len(current) - 1, tk.END) + entry.insert(tk.END, value) + elif value == '.': + return + else: + entry.insert(tk.END, value) + +def clear(): + """Clears the entire entry field.""" + entry.delete(0, tk.END) + +def calculate(): + """Calculates the result of the expression in the entry field.""" + expression = entry.get() + try: + result = eval(expression) + entry.delete(0, tk.END) + entry.insert(0, str(result)) + except ZeroDivisionError: + entry.delete(0, tk.END) + entry.insert(0, "Div/0 Error") + except Exception as e: + entry.delete(0, tk.END) + entry.insert(0, "Error") +root = tk.Tk() +root.title("Modern Calculator") +root.geometry("340x450") +root.resizable(False, False) +root.configure(bg="#2c3e50") +FONT_STYLE = ('Arial', 18, 'bold') +BG_DEFAULT = '#34495e' +FG_DEFAULT = 'white' +BG_OPERATOR = '#e67e22' +BG_EQUALS = '#27ae60' +BG_CLEAR = '#e74c3c' +entry = tk.Entry( + root, + width=15, + font=('Arial', 24), + borderwidth=5, + relief="flat", + justify="right", + bg="#ecf0f1", + fg="#2c3e50" +) +entry.grid(row=0, column=0, columnspan=4, padx=10, pady=15, sticky="nsew") +buttons_data = [ + ('C', 1, 0, 1, BG_CLEAR, clear), + ('/', 1, 3, 1, BG_OPERATOR, lambda: click_button('/')), + ('7', 2, 0, 1, BG_DEFAULT, lambda: click_button('7')), + ('8', 2, 1, 1, BG_DEFAULT, lambda: click_button('8')), + ('9', 2, 2, 1, BG_DEFAULT, lambda: click_button('9')), + ('*', 2, 3, 1, BG_OPERATOR, lambda: click_button('*')), + ('4', 3, 0, 1, BG_DEFAULT, lambda: click_button('4')), + ('5', 3, 1, 1, BG_DEFAULT, lambda: click_button('5')), + ('6', 3, 2, 1, BG_DEFAULT, lambda: click_button('6')), + ('-', 3, 3, 1, BG_OPERATOR, lambda: click_button('-')), + ('1', 4, 0, 1, BG_DEFAULT, lambda: click_button('1')), + ('2', 4, 1, 1, BG_DEFAULT, lambda: click_button('2')), + ('3', 4, 2, 1, BG_DEFAULT, lambda: click_button('3')), + ('+', 4, 3, 1, BG_OPERATOR, lambda: click_button('+')), + ('0', 5, 0, 2, BG_DEFAULT, lambda: click_button('0')), # Span 2 columns + ('.', 5, 2, 1, BG_DEFAULT, lambda: click_button('.')), + ('=', 5, 3, 1, BG_EQUALS, calculate), +] +for i in range(1, 6): # Rows 1 to 5 + root.grid_rowconfigure(i, weight=1) +for i in range(4): # Columns 0 to 3 + root.grid_columnconfigure(i, weight=1) + +for (text, row, col, colspan, bg_color, command) in buttons_data: + btn = tk.Button( + root, + text=text, + font=FONT_STYLE, + bg=bg_color, + fg=FG_DEFAULT, + relief="flat", + command=command + ) + btn.grid( + row=row, + column=col, + columnspan=colspan, + padx=5, + pady=5, + sticky="nsew" + ) + if text == 'C': + btn.grid(columnspan=3) + + +root.mainloop() \ No newline at end of file From 0a3ca826b1fc0b1b305313fb142f3d5348f650a7 Mon Sep 17 00:00:00 2001 From: Akshat Chauhan Date: Wed, 29 Oct 2025 16:41:06 +0530 Subject: [PATCH 2/2] added GUI Calculator --- .../calculator.py => GUI_Calculator/GUI_Calculator.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) rename tinker_calculator/calculator.py => GUI_Calculator/GUI_Calculator.py (95%) diff --git a/tinker_calculator/calculator.py b/GUI_Calculator/GUI_Calculator.py similarity index 95% rename from tinker_calculator/calculator.py rename to GUI_Calculator/GUI_Calculator.py index 2adf1d15..2bf8f6db 100644 --- a/tinker_calculator/calculator.py +++ b/GUI_Calculator/GUI_Calculator.py @@ -70,13 +70,13 @@ def calculate(): ('2', 4, 1, 1, BG_DEFAULT, lambda: click_button('2')), ('3', 4, 2, 1, BG_DEFAULT, lambda: click_button('3')), ('+', 4, 3, 1, BG_OPERATOR, lambda: click_button('+')), - ('0', 5, 0, 2, BG_DEFAULT, lambda: click_button('0')), # Span 2 columns + ('0', 5, 0, 2, BG_DEFAULT, lambda: click_button('0')), ('.', 5, 2, 1, BG_DEFAULT, lambda: click_button('.')), ('=', 5, 3, 1, BG_EQUALS, calculate), ] -for i in range(1, 6): # Rows 1 to 5 +for i in range(1, 6): root.grid_rowconfigure(i, weight=1) -for i in range(4): # Columns 0 to 3 +for i in range(4): root.grid_columnconfigure(i, weight=1) for (text, row, col, colspan, bg_color, command) in buttons_data: @@ -99,6 +99,4 @@ def calculate(): ) if text == 'C': btn.grid(columnspan=3) - - root.mainloop() \ No newline at end of file