This is a simple graphical user interface (GUI) calculator built using Python's Tkinter library. It supports basic arithmetic operations: addition (+), subtraction (-), multiplication (*), and division (/). The calculator features a clean interface with numeric buttons, operator buttons, a clear button, and a decimal point button.
- Basic Operations: Perform addition, subtraction, multiplication, and division.
- Error Handling: Displays "Error" for invalid expressions (e.g., division by zero).
- Clear Functionality: Reset the display and expression.
- Chaining Calculations: After evaluating, the result can be used for further operations.
- User-Friendly Interface: Buttons are uniformly styled with red background and black text on a light green window.
- Python 3.x
- Tkinter (included with standard Python installations)
- Save the code in a file named
simple_calculator.py. - Open a terminal or command prompt.
- Navigate to the directory containing the file.
- Run the command:
python simple_calculator.py. - The calculator window will appear.
- Click numeric buttons (0-9) to input numbers.
- Click operator buttons (+, -, *, /) to input operations.
- Click '.' to add a decimal point.
- Click '=' to evaluate the expression.
- Click 'Clear' to reset the calculator.
- Class Structure: The calculator logic is encapsulated in a
SimpleCalculatorclass for better organization. - Display: Uses a
StringVarlinked to anEntrywidget for input/output. - Buttons: Created dynamically with a helper method
_create_buttonfor consistency. - Expression Handling: Builds an expression string and evaluates it using
eval()with basic error catching. - Layout: Uses grid layout for button placement, with adjusted geometry to fit all elements properly.
- Wrapped in a class for modularity and to avoid global variables.
- Adjusted window height to accommodate the decimal button without overlap.
- Added padding to buttons for better spacing.
- Improved error handling to catch all exceptions.
- Allowed result to be used for further calculations (chaining).
- Justified display text to the right for a more natural calculator feel.
- Does not support advanced operations like exponents or parentheses.
- Uses
eval(), which is simple but not secure for untrusted input (safe here as input is controlled). - No keyboard input support.