Skip to content

Commit

Permalink
Merge pull request #10 from Mmabiaa/Update-Branch
Browse files Browse the repository at this point in the history
Fixed #4 Memory function issue
  • Loading branch information
Mmabiaa authored Jan 7, 2025
2 parents ada8cfb + f379682 commit c8871f9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 27 deletions.
87 changes: 60 additions & 27 deletions assets/py/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import math # Importing math module for mathematical functions
from calculator import Calculator
from utils import show_error # Ensure you have this utility for error handling
from memory import Memory # Importing the Memory class

class CalculatorApp:
def __init__(self):
self.root = Tk()
self.calculator = Calculator()
self.memory = Memory() # Initialize Memory instance
self.setup_ui()

def setup_ui(self):
Expand All @@ -20,25 +22,27 @@ def setup_ui(self):
entry = Entry(self.root, font=("Arial", 24), borderwidth=5, relief="ridge")
entry.grid(row=0, column=0, columnspan=6, padx=10, pady=10)
self.entry = entry

# Create buttons for the calculator
self.create_buttons(self.root)

def create_buttons(self, root):
# A list of Buttons
# A list of Buttons including memory functions
buttons = [
('+', 1, 0), ('-', 1, 1), ('*', 1, 2), ('/', 1, 3), ('%', 1, 4), ('√', 1, 5),
('7', 2, 0), ('8', 2, 1), ('9', 2, 2), ('cos', 2, 3), ('sin', 2, 4), ('tan', 2, 5),
('4', 3, 0), ('5', 3, 1), ('6', 3, 2), ('pi', 3, 3), ('x²', 3, 4), ('1/x', 3, 5),
('1', 4, 0), ('2', 4, 1), ('3', 4, 2), ('!', 4, 3), ('(', 4, 4), (')', 4, 5),
('0', 5, 0), ('.', 5, 1), ('log', 5, 2), ('DEL', 5, 3), ('=', 5, 4), ('C', 5, 5)
('M+', 1, 0), ('M-', 1, 1), ('MR', 1, 2), ('MC', 1, 3), ('%', 1, 4), ('√', 1, 5),
('*', 2, 0), ('/', 2, 1), ('+', 2, 2), ('-', 2, 3), ('(', 2, 4), (')', 2, 5),
('sin', 3, 0), ('7', 3, 1), ('8', 3, 2), ('9', 3, 3), ('x²', 3, 4), ('pi', 3, 5),
('cos', 4, 0), ('4', 4, 1), ('5', 4, 2), ('6', 4, 3), ('1/x', 4, 4), ('!', 4, 5),
('tan', 5, 0), ('1', 5, 1), ('2', 5, 2), ('3', 5, 3), ('=', 5, 4), ('.', 5,5),
('DEL',6 ,1),('0',6 ,2),('C',6 ,3)
]

# A for loop to help arrange the buttons in text, rows and columns
for (text, row, col) in buttons:
for (text,row,col) in buttons:
if text == '=':
bg_color = 'red'
fg_color = 'white'
bd = 20
bd =20
elif text == 'DEL':
bg_color = 'orange'
fg_color = 'white'
Expand All @@ -48,45 +52,72 @@ def create_buttons(self, root):
elif text in ['+', '-', '*', '/']:
bg_color = 'orange'
fg_color = 'black'
elif text in ['1', '2', '3', '4','5', '6','7', '8', '9', '0', '.']:
bg_color = 'grey'
fg_color = 'white'
elif text in ['cos', 'sin', 'tan', 'pi', 'x²', '1/x', '!', '(', ')', 'log']:
elif text in ['1','2','3','4','5','6','7','8','9','0']:
bg_color = 'aqua'
fg_color = 'black'
elif text in ['cos','sin','tan','pi','x²','1/x','!','(',')', '√', '%','.']:
bg_color = 'black'
fg_color = 'white'
else:
bg_color = 'white'
fg_color = 'black'
bg_color = 'red'
fg_color = 'white'

# Creating a button using the variable name 'button'
button = Button(root,
text=text,
font=("Arial", 18),
font=("Arial",18),
command=lambda t=text: self.on_button_click(t),
fg=fg_color,
bg=bg_color
)
button.grid(row=row, column=col, padx=5, pady=5, sticky="nsew")
button.grid(row=row,column=col,padx=5,pady=5,sticky="nsew")

# Allow the grid to be positioned relatively that when the window is resized
for i in range(1,6):
root.grid_rowconfigure(i, weight=1)
root.grid_columnconfigure(i-1, weight=1)
for i in range(1 ,6):
root.grid_rowconfigure(i , weight=1)
root.grid_columnconfigure(i-1 , weight=1)

def on_button_click(self, char):
def on_button_click(self,char):
if char == "=":
try:
equation = str(eval(self.entry.get()))
self.entry.delete(0, END)
self.entry.insert(END, equation)
self.entry.delete(0 , END)
self.entry.insert(END , equation)
except Exception as e:
messagebox.showerror("Error", "Invalid Expression")
messagebox.showerror("Error" , "Invalid Expression")
self.clear()
elif char == "DEL":
self.backspace() # Fixed method name from Backspace to backspace
self.backspace()
elif char == "C":
self.clear()
elif char in ['sin', 'cos', 'tan', '√', 'x²', '1/x', 'log', 'pi', '!', '%']:

# Memory operations
elif char == "M+":
try:
value_to_add = float(self.entry.get())
self.memory.add(value_to_add)
messagebox.showinfo("Memory", f"Added {value_to_add} to memory.")
except ValueError:
messagebox.showerror("Error", "Invalid Input for M+")

elif char == "M-":
try:
value_to_subtract = float(self.entry.get())
self.memory.subtract(value_to_subtract)
messagebox.showinfo("Memory", f"Subtracted {value_to_subtract} from memory.")
except ValueError:
messagebox.showerror("Error", "Invalid Input for M-")

elif char == "MR":
recalled_value = self.memory.recall()
self.entry.delete(0 , END)
self.entry.insert(END , recalled_value)

elif char == "MC":
self.memory.clear()
messagebox.showinfo("Memory", "Memory cleared.")

elif char in ['sin' ,'cos' ,'tan' ,'√' ,'x²' ,'1/x' ,'log' ,'pi' ,'!' ,'%']:
try:
if char == 'sin':
equation = str(math.sin(math.radians(float(self.entry.get()))))
Expand All @@ -108,12 +139,14 @@ def on_button_click(self, char):
equation = str(math.factorial(int(self.entry.get())))
elif char == '%':
equation = str(float(self.entry.get()) /100)

self.entry.delete(0 , END)
self.entry.insert(END , equation)

except Exception as e:
messagebox.showerror("Error", "Invalid Input")
messagebox.showerror("Error" , "Invalid Input")
self.clear()

else:
self.entry.insert(END , char)

Expand Down
21 changes: 21 additions & 0 deletions assets/py/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# memory.py

class Memory:
def _init_(self):
self.value = 0

def add(self, amount):
"""Add amount to memory."""
self.value += amount

def subtract(self, amount):
"""Subtract amount from memory."""
self.value -= amount

def recall(self):
"""Return the current value in memory."""
return self.value

def clear(self):
"""Clear the memory."""
self.value = 0

0 comments on commit c8871f9

Please sign in to comment.