A simple and elegant GUI Calculator built using Pythonโs Tkinter library. This calculator supports basic arithmetic operations along with percentage handling, delete, and clear functionalities โ all wrapped in a clean UI.
โ
Basic arithmetic operations โ +, -, x, /
โ
Percentage calculations (%) with enhanced logic
โ
C (clear) and del (delete last character) functions
โ
Error handling for invalid expressions
โ
Read-only display to prevent unwanted edits
โ
Modern layout with clear, large buttons
The calculator uses a single event handler function (calculator) that binds to all buttons.
When a button is clicked, the text from that button is fetched and evaluated through logic that:
- Builds expressions dynamically.
- Replaces
xwith*for Pythonโs evaluation. - Converts percentage (
%) into valid mathematical expressions using regex. - Evaluates safely using
eval()inside atry-exceptblock.
The app supports expressions like:
| Expression | Interpreted As | Result |
|---|---|---|
50% |
50/100 | 0.5 |
10%100 |
(10/100) * 100 | 10 |
50+10%50 |
50 + (10/100)*50 | 55 |
This makes the percentage operation work more like a real calculator.
-
Entry widget (display): Shows the expression/result
-
Button grid (5x4): Number keys, operators, and function keys
-
Color-coded buttons:
- Orange โ Operator or control (
C,%,/,x,-,+,=) - Gray โ Number buttons
- Orange โ Operator or control (
- Python 3.x
- Tkinter (comes pre-installed with Python)
-
Clone this repository:
git clone https://github.com/LakshyaSharma2005/tkinter-calculator.git
-
Navigate into the folder:
cd tkinter-calculator -
Run the script:
python calculator.py
Lakshya Sharma ๐ GitHub: @LakshyaSharma2005 ๐ฌ Contributions, suggestions, and forks are welcome!
This project is open-source and available under the MIT License.