https://github.com/misaghmomenib/calculator-python
A Simple Yet Powerful Python-based Calculator for Performing Basic Arithmetic Operations. Lightweight and User-friendly, Perfect for Quick Calculations or as a Foundation for Building More Advanced Mathematical Tools.
https://github.com/misaghmomenib/calculator-python
calculator calculator-application calculator-python git open-source python
Last synced: 9 months ago
JSON representation
A Simple Yet Powerful Python-based Calculator for Performing Basic Arithmetic Operations. Lightweight and User-friendly, Perfect for Quick Calculations or as a Foundation for Building More Advanced Mathematical Tools.
- Host: GitHub
- URL: https://github.com/misaghmomenib/calculator-python
- Owner: MisaghMomeniB
- Created: 2024-10-24T18:50:47.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-07T08:12:09.000Z (10 months ago)
- Last Synced: 2025-02-07T09:23:03.994Z (10 months ago)
- Topics: calculator, calculator-application, calculator-python, git, open-source, python
- Language: Python
- Homepage:
- Size: 33.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ๐งฎ Python GUI Calculator
Welcome to my Python-based GUI calculator project! ๐ This calculator is built using the powerful `tkinter` library and serves as a simple yet fully functional application for solving arithmetic expressions. It supports basic operations like addition, subtraction, multiplication, division, and even parentheses for more complex calculations. ๐ค๐ป
### ๐ Features:
- ๐งโ๐ป **Simple and clean design** using `tkinter` for intuitive use.
- ๐ข Supports basic arithmetic operations: `+`, `-`, `*`, `/`, and more.
- ๐งน **Clear (C)** and **equals (=)** buttons to evaluate results instantly.
- ๐ง **Error handling**: Shows "Error" if the input is invalid.
- ๐ Supports continuous calculations without resetting the application.
### ๐ง How It Works:
This calculator handles user input through a text entry and buttons for numbers and operators. It then evaluates the entered mathematical expressions and displays the result. Below is a breakdown of the implementation:
---
### ๐ Code Explanation:
Let's break down the code, line by line. ๐
1. **Importing Required Libraries**:
```python
from tkinter import Tk, Entry, Button, StringVar
```
- `tkinter`: Python's standard GUI library used for creating window-based applications.
- `StringVar`: A variable that binds to the entry display for real-time updates.
2. **Defining the Calculator Class**:
```python
class Calculator:
```
- The `Calculator` class contains the main logic of our calculator.
3. **Window Configuration**:
```python
def configure_window(self, master):
master.title("Calculator")
master.geometry('357x420+0+0')
master.config(bg='white')
master.resizable(False, False)
```
- The window title is set as "Calculator".
- The window size is defined as 357x420, with a fixed background color and no resizing.
4. **Creating the Display**:
```python
def create_display(self, master):
Entry(master, width=17, bg='#fff', font=('Arial Bold', 28), textvariable=self.equation).place(x=0, y=0)
```
- A text entry is used to display the current input and results.
- The font size and color are customized for readability.
5. **Creating the Buttons**:
```python
def create_buttons(self, master):
buttons = [
('(', 0, 70), (')', 90, 70), ('%', 180, 70), ('C', 270, 70),
('7', 0, 140), ('8', 90, 140), ('9', 180, 140), ('/', 270, 140),
('4', 0, 210), ('5', 90, 210), ('6', 180, 210), ('*', 270, 210),
('1', 0, 280), ('2', 90, 280), ('3', 180, 280), ('-', 270, 280),
('0', 0, 350), ('.', 90, 350), ('+', 180, 350), ('=', 270, 350)
]
for (text, x, y) in buttons:
Button(master, text=text, width=11, height=4, relief='flat', bg='white',
command=lambda txt=text: self.handle_click(txt)).place(x=x, y=y)
```
- A list of tuples represents each button with its text and coordinates.
- Each button, when clicked, calls the `handle_click` method, passing the respective button text.
6. **Handling Button Clicks**:
```python
def handle_click(self, value):
if value == '=':
self.solve()
elif value == 'C':
self.clear()
else:
self.update_entry(value)
```
- If "=" is clicked, it triggers the `solve` method to compute the result.
- If "C" is clicked, it triggers the `clear` method to reset the display.
- For any other button, the `update_entry` method adds the clicked value to the entry.
7. **Updating the Entry Field**:
```python
def update_entry(self, value):
self.entry_value += str(value)
self.equation.set(self.entry_value)
```
- The input (value) is appended to the entry string and updated on the screen.
8. **Clearing the Display**:
```python
def clear(self):
self.entry_value = ''
self.equation.set('')
```
- Clears the screen and resets the input value.
9. **Evaluating the Expression**:
```python
def solve(self):
try:
result = eval(self.entry_value)
self.equation.set(result)
self.entry_value = str(result)
except Exception:
self.equation.set("Error")
self.entry_value = ''
```
- The `eval()` function calculates the result of the expression.
- If thereโs an error (invalid input), it shows "Error" on the display.
10. **Setting Up the Application**:
```python
root = Tk()
calculator = Calculator(root)
root.mainloop()
```
- The main application loop is started, which keeps the window open until closed by the user.
---
### ๐ How to Run:
1. Ensure you have Python installed (version 3.x).
2. Clone this repository to your local machine.
3. Run the `calculator.py` file with:
```bash
python calculator.py
```
4. The calculator will launch as a GUI application where you can input expressions and get results instantly!
---
### ๐ก Conclusion:
This calculator showcases basic but essential functionality for a GUI-based application. It's an excellent project for anyone learning Python and `tkinter`. ๐ Feel free to fork it, modify it, or even contribute by adding new features!
---
### ๐ Contributions:
Feel free to open issues, suggest features, or submit pull requests to enhance this project! ๐ฑ
---
Enjoy calculating! โก