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
Metadata
Release files for calculator-ui 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| calculator_ui-0.1.0.tar.gz | 6.3 kB | Without provenance | Details for calculator_ui-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| calculator_ui-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for calculator_ui-0.1.0-py3-none-any.whl |
Total release size: 13.4 kB
Release files / calculator_ui-0.1.0.tar.gz
| Download URL | calculator_ui-0.1.0.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bff0329bc98c0a3c07b8595ad0cd91aaaccc1150d45a208ad9e29e0e3345e62b
|
|
BLAKE2b-256 checksum How to use checksums |
f731092d2bb9520ae2f9cd5b026fe60273ebf9c4a7917f12a2c577a8cd113f70
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.2
|
Release files / calculator_ui-0.1.0-py3-none-any.whl
| Download URL | calculator_ui-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bdd6bb337818961e0520a395ba1bdf5fcca94f713504cde9b07275c86f368bb5
|
|
BLAKE2b-256 checksum How to use checksums |
611eb90e5072719880acee2a75c713de7654f36f7a89b54a0c7b2cd9f7abe458
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.2
|
Security for calculator-ui 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.