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
330×450 dark-themed calculator window titled “Calculadora”. The window supports both mouse clicks and keyboard input.
CalculatorGUI class
Constructor
- Sets the window title to
"Calculadora". - Fixes the geometry to
330×450pixels. - Sets the background colour to
#1E1E1E(dark grey). - Calls
create_widgets()to build the display, error label, and all buttons. - Binds
<Key>events onroottohandle_keypress()for keyboard support.
State attributes
The following instance attributes track the calculator’s runtime state. They are reset byclear_click() and show_error().
| Attribute | Type | Description |
|---|---|---|
expression | str | Full expression string accumulated in parenthesis mode (e.g. "(2+3)*4"). |
current_value | str | Number string currently being entered by the user (e.g. "123" or "-0.5"). |
operator | str | None | Pending operator in normal two-operand mode (+, -, *, /, ^, max, min). None when no operator is pending. |
first_number | float | None | First operand stored after the user presses an operator button in normal mode. None until the first operator is chosen. |
use_expression_mode | bool | True 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").
A single digit character (
"0" – "9").decimal_click()
Appends a decimal point to the current input with defensive validation:
- If
current_valueis empty, sets it to"0.". - If
current_valueis"-", sets it to"-0."to produce a valid float prefix. - If a
.already exists incurrent_value, the click is silently ignored (prevents"3.1.4"). - In expression mode, the
.is appended directly toexpressionwithout these checks.
operation_click(operation: str)
Stores the operator and first operand, enabling a subsequent equals_click() to complete the calculation.
One of
+, -, *, /, ^, max, or min.- If
-is pressed whilecurrent_valueis 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.
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 = Nonefirst_number = Noneexpression = ""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_modeis automatically set back toFalse.
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.
One of
"abs", "max", or "min"."abs"→ callsunary_operation("abs")"max"/"min"→ callsoperation_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.
Currently only
"abs" is active. The value is passed to abs_value() from calculator.py.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.
Human-readable error description. Displayed as
"⚠️ {message}".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 |
|---|---|
0 – 9 | number_button_click(key) |
+, -, *, /, ^ | operation_click(key) |
. | decimal_click() |
( | open_parenthesis_click() |
) | close_parenthesis_click() |
Enter / = | equals_click() |
Escape | clear_click() |
Backspace | backspace_click() |
main() function
tk.Tk() root window, instantiates CalculatorGUI(root), and starts the tkinter event loop with root.mainloop().
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.