A feature-rich scientific calculator application with a modern GUI built in Python using Tkinter. Supports basic arithmetic operations, trigonometric functions, logarithms, and advanced mathematical operations.
Created by: Zain Aftab
- Addition, Subtraction, Multiplication, Division
- Percentage calculations
- Clear (C) and Clear Entry (CE) functions
- Backspace for removing last digit
- Trigonometry: sin, cos, tan, asin, acos, atan
- Logarithms: log₂, log₁₀, natural logarithm (ln)
- Powers & Roots: x², x³, x^n (custom power), √ (square root)
- Advanced: Reciprocal (1/x), Factorial (!)
- Constants: π (pi) and e
- Support for parentheses in expressions
- Input validation for all operations
- Error handling for edge cases (division by zero, invalid domain values)
- Full error messages for better user experience
- Prevention of consecutive operators
- Decimal point validation
- Dark theme with color-coded buttons
- Large, readable display with green text on dark background
- Responsive grid layout
- Button categories:
- Red buttons: Clear and Equals operations
- Teal buttons: Arithmetic operators
- Gray buttons: Scientific functions
- White buttons: Numbers and decimal
- Python 3.6 or higher
- Tkinter (usually comes pre-installed with Python)
python -m tkinterIf a small window opens, Tkinter is installed.
- Download or clone the project
- Ensure Python is installed on your system
- No additional packages needed - Tkinter is included with Python
# Create a virtual environment
python -m venv venv
# Activate it (Windows)
venv\Scripts\activate
# Activate it (macOS/Linux)
source venv/bin/activate- Open the project folder in VS Code
- Press `Ctrl + `` (backtick) to open the Terminal
- Run the calculator:
python scripts/scientific_calculator.py
- Right-click on
scientific_calculator.pyin the file explorer - Select "Run Python File in Terminal"
- Click on the Python file
- Click the ▶ Run button in the top-right corner
- Select your Python interpreter if prompted
Using Mouse:
- Click any button to add it to the display
- Click equals (=) to calculate the result
Using Keyboard:
- Type numbers: 0-9
- Type operators:
+,-,*,/,% - Type parentheses:
(,) - Press
Enteror=to calculate - Press
Backspaceto delete last digit - Press
Escapeto clear all (like C button)
- Basic:
5 + 3 =→ Result:8 - Powers:
2 x^n 10 =→ Result:1024 - Trigonometry:
sin(π/2) =→ Result:1.0 - Logarithms:
log₁₀(100) =→ Result:2.0 - Complex:
(√16 + 5) × 2 =→ Result:18.0
| Key | Function |
|---|---|
0-9 |
Enter digits |
+, -, *, /, % |
Operators |
(, ) |
Parentheses |
Enter or = |
Calculate |
Backspace |
Delete last digit |
Escape |
Clear all (C) |
The calculator gracefully handles:
- Division by zero: Shows "Error: Cannot divide by zero"
- Invalid trigonometric input: Shows "Error: asin/acos input must be between -1 and 1"
- Negative logarithms: Shows "Error: Cannot take log of non-positive number"
- Negative square root: Shows "Error: Cannot take sqrt of negative number"
- Large factorials: Limited to 170 to prevent overflow
- Mismatched parentheses: Shows "Error: Mismatched parentheses"
- Incomplete expressions: Shows "Error: Incomplete expression"
- Class-based design:
ScientificCalculatorclass encapsulates all functionality - Safe evaluation: Uses restricted namespace for eval() to prevent security issues
- State tracking: Tracks operator state to prevent invalid inputs
- Error propagation: All errors are caught and displayed to the user
- Background:
#1e1e1e(Dark Gray) - Display:
#2d2d2d(Darker Gray) with#00ff00(Green text) - Buttons:
- Standard:
#3d3d3d(Medium Gray) - Operators:
#17a2b8(Teal) - Clear/Equals:
#dc3545(Red) - Scientific:
#6c757d(Dark Gray)
- Standard:
- math module: For trigonometric, logarithmic, and factorial functions
Solution: Install tkinter
# Windows
pip install tk
# macOS
brew install python-tk
# Linux
sudo apt-get install python3-tkSolution: Check Python path in VS Code
- Go to View → Command Palette (Ctrl+Shift+P)
- Type "Python: Select Interpreter"
- Choose the correct Python version
Solution: Click the calculator window to ensure it has focus, then try again
scientific-calculator/
├── scripts/
│ └── scientific_calculator.py # Main calculator application
├── README.md # This file
└── [other project files]
This project is open-source and available for educational and personal use.
Author: Zain Aftab
For issues, questions, or suggestions, feel free to reach out.
- Initial release
- 40+ scientific functions
- Full error handling
- Dark theme GUI
- Keyboard support
- History of calculations
- Conversion tools (units, bases)
- Custom variables
- Graphing functions
- Scientific notation display
- Undo/Redo functionality
Enjoy your scientific calculator! Happy calculating! 🧮