Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/WorkTeam01/team-practice/llms.txt

Use this file to discover all available pages before exploring further.

src/gui.py defines the CalculatorGUI class, which builds and manages the tkinter calculator window. It delegates all arithmetic to src/calculator.py and supports both normal (two-operand) and expression (parenthesized) calculation modes. Keyboard input is supported alongside mouse clicks, and all errors are surfaced through an on-screen error label rather than exceptions.

Running the GUI

python src/gui.py
This creates a 330×450 dark-themed calculator window titled “Calculadora”. The window supports both mouse clicks and keyboard input.

CalculatorGUI class

Constructor

CalculatorGUI(root: tk.Tk)
Takes a tkinter root window and fully initialises the calculator. On construction it:
  • Sets the window title to "Calculadora".
  • Fixes the geometry to 330×450 pixels.
  • Sets the background colour to #1E1E1E (dark grey).
  • Calls create_widgets() to build the display, error label, and all buttons.
  • Binds <Key> events on root to handle_keypress() for keyboard support.
import tkinter as tk
from src.gui import CalculatorGUI

root = tk.Tk()
app = CalculatorGUI(root)
root.mainloop()

State attributes

The following instance attributes track the calculator’s runtime state. They are reset by clear_click() and show_error().
AttributeTypeDescription
expressionstrFull expression string accumulated in parenthesis mode (e.g. "(2+3)*4").
current_valuestrNumber string currently being entered by the user (e.g. "123" or "-0.5").
operatorstr | NonePending operator in normal two-operand mode (+, -, *, /, ^, max, min). None when no operator is pending.
first_numberfloat | NoneFirst operand stored after the user presses an operator button in normal mode. None until the first operator is chosen.
use_expression_modeboolTrue when a parenthesis has been opened and the calculator is accumulating a full expression string. Reverts to False after = is evaluated or clear_click() is called.

Public methods

create_widgets()

Builds the entire calculator UI: creates the main display entry widget, the error label, and all calculator buttons with their colours and click handlers. Also configures row and column weights so the layout expands correctly. Called automatically by __init__() — no need to invoke it directly.

number_button_click(valor: str)

Appends a single digit to the current input. In expression mode the digit is appended to expression; in normal mode it is appended to current_value. Non-digit values trigger show_error("Número inválido").
valor
str
required
A single digit character ("0""9").

decimal_click()

Appends a decimal point to the current input with defensive validation:
  • If current_value is empty, sets it to "0.".
  • If current_value is "-", sets it to "-0." to produce a valid float prefix.
  • If a . already exists in current_value, the click is silently ignored (prevents "3.1.4").
  • In expression mode, the . is appended directly to expression without these checks.

operation_click(operation: str)

Stores the operator and first operand, enabling a subsequent equals_click() to complete the calculation.
operation
str
required
One of +, -, *, /, ^, max, or min.
Behaviour details:
  • If - is pressed while current_value is empty, it begins a negative number (current_value = "-").
  • If an operator is pressed while a previous calculation is pending, equals_click() is called first to chain the result.
  • In expression mode, the operator is appended directly to expression.

equals_click()

Evaluates the current expression or two-operand calculation and updates the display. Normal mode — requires first_number, operator, and current_value to all be set. Dispatches to the matching function from calculator.py and displays the result. After evaluation, first_number and operator are reset to None. Expression mode — validates the accumulated expression string, replaces ^ with **, then calls Python’s built-in eval(). On success the result is shown in the display and use_expression_mode is set back to False.
If first_number and operator are set but current_value is empty, show_error("Ingresa el segundo número") is called instead of evaluating.

clear_click()

Resets all state and clears both the main display and the error label. Equivalent to pressing C on a physical calculator. After calling clear_click() the following attributes are guaranteed:
  • current_value = ""
  • operator = None
  • first_number = None
  • expression = ""
  • use_expression_mode = False

backspace_click()

Removes the last character from the current input. Does nothing if the input is already empty.
  • Normal mode — removes the last character from current_value.
  • Expression mode — removes the last character from expression. If no parentheses remain after deletion, use_expression_mode is automatically set back to False.

open_parenthesis_click()

Activates expression mode and appends ( to the expression string. If a partial normal-mode calculation is already in progress (i.e. first_number and/or current_value are set), those values are transferred into expression before the ( is appended, preserving context.

close_parenthesis_click()

Appends ) to the expression string. use_expression_mode is set to True if it was not already.

scientific_click(func: str)

Dispatches scientific function button clicks to the correct handler.
func
str
required
One of "abs", "max", or "min".
  • "abs" → calls unary_operation("abs")
  • "max" / "min" → calls operation_click(func), treating them as binary operators that require a second operand before = is pressed.

unary_operation(func: str)

Applies a unary function directly to current_value and updates the display immediately. No second operand or = press is required.
func
str
required
Currently only "abs" is active. The value is passed to abs_value() from calculator.py.
Calls show_error("Número incompleto") if current_value is exactly "-" (an incomplete negative number).

show_error(message: str)

Displays an error in the error label and resets all calculator state so the user can start fresh.
message
str
required
Human-readable error description. Displayed as "⚠️ {message}".
The error label is automatically cleared after 3 seconds via root.after(3000, ...). Alongside displaying the message, this method also resets current_value, first_number, operator, expression, use_expression_mode, and the display entry widget.

handle_keypress(event)

Keyboard event handler bound to the root window via root.bind('<Key>', self.handle_keypress). Maps physical key presses to their corresponding calculator actions:
Key(s)Action
09number_button_click(key)
+, -, *, /, ^operation_click(key)
.decimal_click()
(open_parenthesis_click()
)close_parenthesis_click()
Enter / =equals_click()
Escapeclear_click()
Backspacebackspace_click()

main() function

def main() -> None
The module-level entry point. Creates a tk.Tk() root window, instantiates CalculatorGUI(root), and starts the tkinter event loop with root.mainloop().
def main():
    root = tk.Tk()
    app = CalculatorGUI(root)
    root.mainloop()

if __name__ == "__main__":
    main()

CalculatorGUI uses Python’s built-in eval() for expression mode, but only after the expression string passes a strict regex check: ^[\d\s\+\-\*\/\^\(\)\.]+$. This allows only digits, whitespace, the five arithmetic operators, parentheses, and decimal points — preventing code injection attacks while still supporting complex parenthesized expressions.

Build docs developers (and LLMs) love