A calculator ui
Project description
Calculator UI
基于 Python 与 Tkinter 的图形化计算器,采用面向对象设计,将计算逻辑与界面展示分离为独立类,并支持鼠标与键盘操作。
功能特性
- 四则运算:
+、-、×、÷ - 小数输入、取反(±)、百分号(%)
- 全部清除(C)、清除当前输入(CE)、退格(⌫)
- 除零等异常时显示错误提示
- 深色主题界面
- 完整键盘快捷键支持
环境要求
- Python 3.10 及以上
- 标准库 tkinter(Windows 安装 Python 时通常已包含;部分 Linux 需单独安装
python3-tk)
快速开始
直接运行
cd pip_package
python main.py
作为包安装(可选)
pip install -e .
安装后可在代码中引用:
from calculator import CalculatorEngine, CalculatorUI
app = CalculatorUI()
app.run()
键盘快捷键
| 按键 | 功能 |
|---|---|
0–9 |
输入数字 |
. |
小数点 |
+ - * / |
运算符 |
Enter 或 = |
等于 |
Esc 或 Delete |
全部清除(C) |
Backspace |
退格 |
N |
取反(±) |
% |
百分号 |
项目结构
pip_package/
├── main.py # 程序入口
├── setup.py # 打包配置
├── requirements.txt
├── README.md
└── calculator/
├── __init__.py
├── engine.py # CalculatorEngine:计算逻辑
└── ui.py # CalculatorUI:图形界面与键盘事件
架构说明
项目遵循关注点分离原则:
| 类 | 文件 | 职责 |
|---|---|---|
CalculatorEngine |
calculator/engine.py |
维护显示状态、处理运算与输入,不依赖任何 GUI 库 |
CalculatorUI |
calculator/ui.py |
构建 Tkinter 窗口、绑定按钮与键盘,调用引擎并刷新显示 |
CalculatorUI 可接收外部传入的 CalculatorEngine 实例,便于单独测试计算逻辑:
from calculator.engine import CalculatorEngine
engine = CalculatorEngine()
engine.input_digit("1")
engine.input_digit("2")
engine.input_operator("+")
engine.input_digit("3")
engine.calculate()
print(engine.display) # 15
开发与打包
# 以可编辑模式安装,便于本地开发
pip install -e .
# 构建分发包
python setup.py sdist bdist_wheel
许可证
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
calculator_ui-0.1.0.tar.gz
(6.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file calculator_ui-0.1.0.tar.gz.
File metadata
- Download URL: calculator_ui-0.1.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bff0329bc98c0a3c07b8595ad0cd91aaaccc1150d45a208ad9e29e0e3345e62b
|
|
| MD5 |
fc594591dc04b6a117b0568ec6bcbec0
|
|
| BLAKE2b-256 |
f731092d2bb9520ae2f9cd5b026fe60273ebf9c4a7917f12a2c577a8cd113f70
|
File details
Details for the file calculator_ui-0.1.0-py3-none-any.whl.
File metadata
- Download URL: calculator_ui-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bdd6bb337818961e0520a395ba1bdf5fcca94f713504cde9b07275c86f368bb5
|
|
| MD5 |
fa8c62d9c4745891b137d3c99343d02d
|
|
| BLAKE2b-256 |
611eb90e5072719880acee2a75c713de7654f36f7a89b54a0c7b2cd9f7abe458
|