Math & Physics Toolkit – GUI Technical Documentation
1. Overview
The Math & Physics Toolkit is a comprehensive desktop application designed to perform calculations across 27 domains of mathematics and physics. It features a minimalist, dark-themed Graphical User Interface (GUI) built with Python’s tkinter library, alongside an embedded Command Line Interface (CLI) terminal for advanced users.
This document details the architecture, module structure, user interface components, and operational logic of gui_app.py.
2. System Architecture
2.1 Core Technology Stack
- Language: Python 3
- GUI Framework:
tkinter(standard library) - Styling: Custom dark theme using hexadecimal color constants.
- Module Loading: Dynamic import via
importlibfor lazy loading of calculation modules. - CLI Integration:
subprocessmodule to execute external CLI commands (main.py) from within the GUI.
2.2 Directory Structure Assumptions
The GUI assumes the following project structure:
project_root/
├── gui_app.py # The GUI application (this file)
├── main.py # The CLI entry point
└── src/ # Package containing calculation modules
├── mechanics.py
├── thermo.py
├── electromagnetism.py
├── ...
└── abstract_algebra.py
3. Configuration & Constants
3.1 Theme Constants
The application uses a consistent dark theme defined by the following constants:
- Backgrounds:
BG(#1a1a2e),BG2(#16213e),BG3(#0f3460) - Foregrounds:
FG(#e0e0e0),FG2(#a0a0b0) - Accents:
ACCENT(#e94560 - Red/Pink),ACCENT2(#533483 - Purple) - Fonts:
FONT_MONO: Consolas, 10pt (for code/results)FONT_SM/MD/LG/HDR: Segoe UI variants for UI elements.
3.2 Domain Registry
The application supports 27 specific domains, mapped between internal keys and display names.
| Internal Key | Display Name | Module Path (src.*) |
|---|---|---|
mechanics |
Classical Mechanics | src.mechanics |
thermo |
Thermodynamics | src.thermo |
em |
Electromagnetism | src.electromagnetism |
optics |
Optics & Photonics | src.optics |
acoustics |
Acoustics | src.acoustics |
relativity |
Relativity | src.relativity |
quantum |
Quantum Mechanics | src.quantum |
atomic |
Atomic & Condensed Matter | src.atomic |
nuclear |
Nuclear & Particle Physics | src.nuclear |
astro |
Astrophysics & Cosmology | src.astrophysics |
nonlinear |
Nonlinear & Chaos | src.nonlinear |
engineering |
Engineering Physics | src.engineering |
specialized |
Specialized Physics | src.specialized |
math-basic |
Basic Mathematics | src.math_basic |
geometry |
Geometry & Trigonometry | src.math_geometry |
discrete |
Discrete Mathematics | src.discrete_math |
probability |
Probability & Statistics | src.probability |
calculus |
Calculus & Analysis | src.calculus |
linalg |
Linear Algebra | src.linear_algebra |
diffeq |
Differential Equations | src.differential_eq |
numtheory |
Number Theory | src.number_theory |
optimization |
Optimization | src.optimization |
numerical |
Numerical Methods | src.numerical |
transforms |
Transforms & Signals | src.transforms |
geo-adv |
Advanced Geometry | src.geometry_advanced |
special-func |
Special Functions | src.special_functions |
ai-math |
AI & ML Mathematics | src.ai_math |
algebra |
Abstract Algebra | src.abstract_algebra |
4. Class Structure: MathPhysicsGUI
The entire application is encapsulated within the MathPhysicsGUI class.
4.1 Initialization (__init__)
- Initializes the main Tk window (
self.root). - Sets window title, geometry (1200x750), and background color.
- Initializes state variables:
commands_cache: Dictionary to store loaded module commands.current_domain/current_command: Tracks user selection.param_entries: Dictionary to hold input field widgets.
- Calls
_build_ui()to construct the interface. - Calls
_load_all_commands()(currently a placeholder; loading is lazy).
4.2 UI Construction (_build_ui)
The UI is divided into three main sections:
- Top Bar:
- Displays the application title "MATH & PHYSICS TOOLKIT".
- Contains buttons for CLI Terminal and About.
- Main Paned Window (
tk.PanedWindow):- Left Pane (Sidebar): Contains the Domain Tree.
- Right Pane (Content Area): Displays command lists, input forms, and results.
- Status Bar:
- Located at the bottom, provides context-sensitive feedback (e.g., "Ready", "Calculation complete").
4.3 Domain Tree (_build_tree)
- Uses
ttk.Treeviewto display a hierarchical list of domains. - Root Nodes: "PHYSICS" and "MATHEMATICS".
- Child Nodes: Specific domains (e.g., "Classical Mechanics", "Calculus").
- Styling: Custom
ttk.Styleconfigured for the dark theme. - Event Binding:
<<TreeviewSelect>>triggers_on_tree_select.
5. Operational Logic
5.1 Lazy Loading (_load_domain_commands)
To optimize performance, modules are not imported at startup. Instead:
- When a user selects a domain in the tree,
_on_tree_selectis called. - It checks
self.commands_cache. - If missing, it uses
importlib.import_module()to load the corresponding module fromsrc/. - It extracts the
COMMANDSdictionary from the module. - Errors during import are caught and displayed via
messagebox.showerror.
5.2 Command Picker (_show_command_picker)
When a domain is selected:
- The right pane is cleared.
- A header displays the Domain Name and command count.
- A scrollable list of commands is generated.
- Each command card shows the Name and Description.
- Parameters are listed briefly (e.g.,
params: [mass, velocity]).
- Clicking a command card triggers
_show_input_form.
5.3 Input Form (_show_input_form)
When a specific command is selected:
- The right pane is cleared.
- A "Back" button is provided to return to the command list.
- Parameter Inputs:
- For each parameter defined in the command's metadata, a labeled
tk.Entrywidget is created. - Labels use monospace font for clarity.
- For each parameter defined in the command's metadata, a labeled
- Action Buttons:
- COMPUTE: Triggers
_run_calculation. - Clear: Resets input fields and result area.
- COMPUTE: Triggers
- Result Area:
- A
scrolledtext.ScrolledTextwidget (read-only) is prepared to display output.
- A
5.4 Calculation Execution (_run_calculation)
- Collects values from
self.param_entries. - Attempts to convert inputs to
float. If conversion fails, the string value is passed (allowing for symbolic or non-numeric inputs if supported by the backend). - Retrieves the function object from the loaded module:
commands[cmd_name]['func']. - Executes the function:
result = func(**params). - Handles exceptions and displays errors via
_display_error. - On success, calls
_display_result.
5.5 Result Display (_display_result)
Formats the output dictionary into a readable string:
────────────────────────────────────────────────
key1 = value1
key2 = value2
────────────────────────────────────────────────
RESULT = calculated_value
UNIT = unit_string
────────────────────────────────────────────────
6. Embedded CLI Terminal
The application includes a feature to open a separate window acting as a CLI terminal.
6.1 Implementation (_open_terminal)
- Creates a
tk.Toplevelwindow. - Contains an output area (
ScrolledText) and an input field (Entry). - Command Execution:
- Uses
subprocess.runto executepython main.py <args>. - Captures
stdoutandstderr. - Displays output in the terminal window.
- Uses
- Special Commands:
help: Displays usage instructions locally without calling subprocess.exit/quit: Closes the terminal window.
6.2 CLI Usage Syntax
The terminal expects commands formatted for the underlying main.py script:
python main.py <domain> <command> --param1 val1 --param2 val2
python main.py list # List all domains
python main.py <domain> list # List commands in a domain
7. Error Handling
- Import Errors: Caught during module loading; displayed in a messagebox.
- Calculation Errors: Caught during
_run_calculation; displayed in the result text area with prefixERROR:. - Timeouts: CLI commands are limited to a 30-second timeout.
- Input Validation: Basic type conversion (string to float) is attempted. Invalid numeric inputs may be passed as strings, relying on the backend module to handle validation or raise errors.
8. Extensibility
To add a new domain or command:
- Create Module: Add a new Python file in
src/(e.g.,src/new_domain.py). - Define COMMANDS: Inside the module, define a
COMMANDSdictionary:python COMMANDS = { 'my-command': { 'desc': 'Description of the command', 'params': ['param1', 'param2'], 'func': my_python_function } } - Update Registry: Add the domain key and display name to
DOMAIN_REGISTRYingui_app.py. - Update Mapping: Add the domain key and module path to
MODULE_MAPingui_app.py. - Update Tree: Add the domain key to either
physics_domainsormath_domainslist in_build_tree.
9. Dependencies
- Standard Library:
tkinter,ttk,scrolledtext,messagebox,fontsys,os,json,subprocess,threading,importlib
- External Libraries:
- None explicitly required for the GUI itself.
- Note: The backend modules in
src/may require libraries likenumpy,scipy, orsympy, but these are not imported ingui_app.py.
10. Version Information
- Application Name: Math & Physics Toolkit
- File:
gui_app.py - Last Updated: Based on provided code snapshot.
- Author: cagent (User Context)
Built With
- batchfile
- python

Log in or sign up for Devpost to join the conversation.