This is a scientific calculator application built using Python's tkinter library. The application provides an intuitive graphical user interface (GUI), allowing users to easily perform basic arithmetic and advanced mathematical functions.
Key Features
Basic Operations: Addition (+), Subtraction (-), Multiplication (×), Division (÷).
Scientific Functions:
Trigonometric: sin(), cos(), tan().
Logarithmic: log() (base 10) and ln() (base e).
Exponents and Roots: x², xʸ, √.
Factorial: x!.
Constants: Uses constants like π and e.
Error Handling: Displays "Lỗi" (Error) for invalid expressions.
Convenient Actions:
"C" button to clear the entire expression.
"DEL" button for backspace functionality.
Security: Uses a SAFE_EVAL_DICT dictionary to limit the functions available to eval(), preventing the execution of malicious code.
How to Use
Installation: Ensure Python is installed on your computer. The application only uses the standard tkinter and math libraries, so no additional installations are required.
Running the Application: Open a terminal or command prompt, navigate to the directory containing the máy tính.py file, and run the following command:
python máy tính.py
Usage:
Use the mouse to click the buttons on the interface to enter an expression.
Press the = button to calculate the result.
Press the C button to clear the display.
Press the DEL button to delete the last character.
Code Structure
on_button_click(char): Handles button presses, appending the corresponding character to the display.
calculate(): The main function for computation. It replaces user-friendly characters (×, ÷, √, ^, !) with Python operators (*, /, sqrt, **, factorial) and uses a safe eval() function to compute the result.
clear_display(): Clears the content on the display.
delete_char(): Deletes the last character.
SAFE_EVAL_DICT: A dictionary containing pre-defined mathematical functions and constants, ensuring that eval() can only execute safe operations.
GUI: Uses tkinter to set up the window, the display Entry, and the Button widgets with a grid layout for an aesthetically pleasing and resizable interface.
