This is a basic calculator application built using PyQt5. It includes functionality for basic arithmetic operations and can be operated using both on-screen buttons and keyboard input. This project demonstrates a simple GUI application with event handling in Python. The UI now supports a dark theme for a modern look and reduced eye strain. An application icon is also included.
- Basic arithmetic operations: addition, subtraction, multiplication, and division.
- Clear and backspace functionalities.
- Keyboard input for calculator operations.
- User-friendly graphical interface with dark theme.
- Application icon for a polished look.
- Clone the repository:
git clone https://github.com/your-username/Basic-Calculator.git
- Change to the project directory:
cd Basic-Calculator
- Install the required packages:
pip install PyQt5
-
Run the Python script:
python Basic-Calculator.py
-
Use the on-screen buttons or the keyboard to perform calculations.
0-9
: Enter numbers+
: Addition-
: Subtraction*
: Multiplication/
: Division(
: Open parenthesis)
: Close parenthesis.
: Decimal pointEnter
: Calculate resultBackspace
: Remove last characterESC
: Clear input
The Basic-Calculator.py
script is the main Python file for this application. It uses the PyQt5 library to create the GUI. Here is a brief explanation of the code:
-
Importing Libraries: The code starts by importing the necessary modules from PyQt5.
from PyQt5 import QtCore, QtGui, QtWidgets
-
UI Setup: The
Ui_Form
class contains the setup for the UI. It defines all the buttons and the input field, and now includes a dark theme and an application icon.class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 300) self.setWindowIcon(QtGui.QIcon('icon.png')) # Set application icon self.setStyleSheet("background-color: #2e2e2e; color: #f0f0f0;") # Dark theme self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setGeometry(QtCore.QRect(10, 10, 381, 41)) self.lineEdit.setObjectName("lineEdit") self.lineEdit.setStyleSheet("background-color: #1e1e1e; color: #ffffff;") ...
-
Button Initialization: Each button is initialized with a specific geometry and text label, and styled for the dark theme.
self.pushButton = QtWidgets.QPushButton(Form, text="7") self.pushButton.setGeometry(QtCore.QRect(10, 60, 61, 51)) self.pushButton.setStyleSheet("background-color: #3e3e3e; color: #ffffff;")
-
Connecting Buttons to Methods: Each button is connected to a method that handles its functionality.
self.pushButton.clicked.connect(lambda: self.button_clicked("7"))
-
Button Click Handler: The
button_clicked
method updates the input field with the button's text.def button_clicked(self, text): current_text = self.lineEdit.text() self.lineEdit.setText(current_text + text)
-
Calculation and Clear Functions: The
calculate
method evaluates the expression in the input field, while theclear
method resets it.def calculate(self): try: result = eval(self.lineEdit.text()) self.lineEdit.setText(str(result)) except Exception as e: self.lineEdit.setText("Error") def clear(self): self.lineEdit.setText("")
-
Keyboard Input Handling: The
keyPressEvent
method allows the user to use the keyboard for input.def keyPressEvent(self, event): key = event.key() ...
- UI File: The user interface is defined within the Python script itself using PyQt5 widgets and layouts.
- EXE File: The executable file (
Basic-Calculator.exe
) allows users to run the application without needing to install Python or the required libraries.
Basic-Calculator.py
: The main Python script for the application.icon.png
: The icon file for the application.Basic-Calculator.exe
: The executable file for the application (if provided).
Hamed Gharghi
This project is licensed under the MIT License - see the LICENSE file for details.
- PyQt5 Documentation: https://www.riverbankcomputing.com/static/Docs/PyQt5/
Python
PyQt5
Calculator
GUI
Basic Arithmetic
Keyboard Input
Event Handling
Open Source
MIT License
Dark Theme
Icon
Stickers: