Scientific calculator
A calculator window with basic and scientific functions, keyboard support and history, without using eval.
What you'll learn
- âTkinter layout with grid
- âEvent handling and key bindings
- âWriting a safe expression evaluator
What you'll build
- âButtons for digits, + â Ã Ãˇ, brackets, sqrt, sin/cos, power
- âKeyboard input
- âHistory panel
- âClear error messages
Step-by-step guide
- 1
Create the window
root = tk.Tk(); add an Entry for the display and a frame of Buttons placed with grid(row, column).
- 2
Wire the buttons
Each button appends its symbol to the display with a command=lambda s=s: press(s).
- 3
Evaluate safely
Do not use eval on user input. Parse with the ast module and allow only number, BinOp, UnaryOp and a whitelist of math functions.
- 4
Add scientific keys
Map sin, cos, tan, log, sqrt to math functions; add a Deg/Rad toggle.
- 5
Keyboard support
root.bind("<Key>", on_key) to accept digits and operators, Enter to evaluate, Backspace to delete.
- 6
History
Show each "expression = result" in a Listbox; clicking an item puts it back on the display.
- 7
Polish
Use ttk styles, a fixed font, and catch ZeroDivisionError to show "Cannot divide by zero".
đ Make it stand out
- +Add a dark theme
- +Package it as an .exe with PyInstaller
- +Add unit conversion