Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit e8b7954

Browse files
add a simple calculator with GUI and history
1 parent d516164 commit e8b7954

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Simple Calculator with History
2+
3+
A modern calculator application built with Python and Tkinter that includes a calculation history feature.
4+
5+
## Features
6+
7+
- Basic arithmetic operations (+, -, *, /)
8+
- Parentheses support for complex expressions
9+
- Calculation history display
10+
- Clear history functionality
11+
- Modern GUI with clean design
12+
- Backspace button to correct mistakes
13+
- Error handling for invalid expressions
14+
15+
## Requirements
16+
17+
- Python 3.x
18+
- Tkinter (usually comes with Python installation)
19+
20+
## How to Run
21+
22+
1. Make sure you have Python installed on your system
23+
2. Navigate to the project directory
24+
3. Run the following command:
25+
```
26+
python calculator.py
27+
```
28+
29+
## Usage
30+
31+
- Use the number buttons (0-9) and operators (+, -, *, /) to create expressions
32+
- Press '=' to calculate the result
33+
- Press 'C' to clear the current expression
34+
- Press '←' to delete the last character
35+
- Use parentheses '(' and ')' for complex expressions
36+
- View calculation history on the right side
37+
- Click "Clear History" to remove all history entries
38+
39+
## Error Handling
40+
41+
The calculator will display "Error" if:
42+
- The expression is invalid
43+
- Division by zero is attempted
44+
- Parentheses are not properly matched
45+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import tkinter as tk
2+
from tkinter import ttk
3+
import math
4+
5+
class Calculator:
6+
def __init__(self, root):
7+
self.root = root
8+
self.root.title("Calculator with History")
9+
self.root.geometry("600x400")
10+
self.root.configure(bg="#f0f0f0")
11+
12+
self.current_expression = ""
13+
self.history = []
14+
15+
self.create_frames()
16+
17+
self.create_display()
18+
19+
self.create_buttons()
20+
21+
self.create_history_display()
22+
23+
def create_frames(self):
24+
25+
self.main_container = ttk.Frame(self.root)
26+
self.main_container.pack(expand=True, fill="both", padx=10, pady=10)
27+
28+
self.calculator_frame = ttk.Frame(self.main_container)
29+
self.calculator_frame.pack(side="left", expand=True, fill="both", padx=(0, 5))
30+
31+
self.history_frame = ttk.Frame(self.main_container)
32+
self.history_frame.pack(side="right", expand=True, fill="both", padx=(5, 0))
33+
34+
def create_display(self):
35+
36+
self.display = ttk.Entry(self.calculator_frame, justify="right", font=("Arial", 20))
37+
self.display.pack(fill="x", padx=5, pady=5)
38+
39+
def create_buttons(self):
40+
41+
button_frame = ttk.Frame(self.calculator_frame)
42+
button_frame.pack(expand=True, fill="both")
43+
44+
buttons = [
45+
('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
46+
('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
47+
('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
48+
('0', 4, 0), ('.', 4, 1), ('=', 4, 2), ('+', 4, 3),
49+
('C', 5, 0), ('←', 5, 1), ('(', 5, 2), (')', 5, 3),
50+
]
51+
52+
for (text, row, col) in buttons:
53+
button = ttk.Button(button_frame, text=text, command=lambda t=text: self.button_click(t))
54+
button.grid(row=row, column=col, sticky="nsew", padx=2, pady=2)
55+
56+
for i in range(6):
57+
button_frame.grid_rowconfigure(i, weight=1)
58+
for i in range(4):
59+
button_frame.grid_columnconfigure(i, weight=1)
60+
61+
def create_history_display(self):
62+
63+
history_label = ttk.Label(self.history_frame, text="History", font=("Arial", 14))
64+
history_label.pack(pady=5)
65+
66+
self.history_listbox = tk.Listbox(self.history_frame, font=("Arial", 12))
67+
self.history_listbox.pack(expand=True, fill="both")
68+
69+
clear_history_btn = ttk.Button(self.history_frame, text="Clear History", command=self.clear_history)
70+
clear_history_btn.pack(pady=5)
71+
72+
def button_click(self, value):
73+
if value == "=":
74+
try:
75+
result = eval(self.current_expression)
76+
77+
history_entry = f"{self.current_expression} = {result}"
78+
self.history.append(history_entry)
79+
self.history_listbox.insert(0, history_entry)
80+
81+
self.current_expression = str(result)
82+
self.display.delete(0, tk.END)
83+
self.display.insert(tk.END, self.current_expression)
84+
except:
85+
self.display.delete(0, tk.END)
86+
self.display.insert(tk.END, "Error")
87+
self.current_expression = ""
88+
elif value == "C":
89+
self.current_expression = ""
90+
self.display.delete(0, tk.END)
91+
elif value == "←":
92+
self.current_expression = self.current_expression[:-1]
93+
self.display.delete(0, tk.END)
94+
self.display.insert(tk.END, self.current_expression)
95+
else:
96+
self.current_expression += value
97+
self.display.delete(0, tk.END)
98+
self.display.insert(tk.END, self.current_expression)
99+
100+
def clear_history(self):
101+
self.history = []
102+
self.history_listbox.delete(0, tk.END)
103+
104+
if __name__ == "__main__":
105+
root = tk.Tk()
106+
app = Calculator(root)
107+
style = ttk.Style()
108+
style.theme_use('clam')
109+
root.mainloop()

0 commit comments

Comments
 (0)