Skip to content

Commit 25fb34e

Browse files
committed
Initial commit: Python Calculator for Android
0 parents  commit 25fb34e

11 files changed

Lines changed: 422 additions & 0 deletions

.github/workflows/build.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Build Android APK
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
workflow_dispatch:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v3
14+
15+
- name: Build with Buildozer
16+
uses: ArtemSBulgakov/buildozer-action@v1
17+
id: buildozer
18+
with:
19+
command: buildozer android debug
20+
buildozer_version: stable
21+
22+
- name: Upload APK
23+
uses: actions/upload-artifact@v3
24+
with:
25+
name: calculator-apk
26+
path: bin/*.apk

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Buildozer files
2+
.buildozer/
3+
bin/
4+
build/
5+
6+
# Python
7+
__pycache__/
8+
*.py[cod]
9+
*$py.class
10+
*.so
11+
.Python
12+
env/
13+
venv/
14+
15+
# IDE
16+
.vscode/
17+
.idea/
18+
*.swp
19+
*.swo
20+
21+
# OS
22+
.DS_Store
23+
Thumbs.db

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Python 计算器 Android APK
2+
3+
这是一个使用 Python 和 Kivy 框架开发的计算器应用。
4+
5+
## 功能特性
6+
7+
- 基本算术运算:加、减、乘、除
8+
- 小数点支持
9+
- 清除功能
10+
- 简洁的用户界面
11+
12+
## 本地测试
13+
14+
1. 安装依赖:
15+
```bash
16+
pip install -r requirements.txt
17+
```
18+
19+
2. 运行应用:
20+
```bash
21+
python main.py
22+
```
23+
24+
## 打包成 Android APK
25+
26+
### 方法一:使用 Buildozer(推荐在 Linux 上使用)
27+
28+
1. 安装 Buildozer:
29+
```bash
30+
pip install buildozer
31+
```
32+
33+
2. 安装必要的系统依赖(Ubuntu/Debian):
34+
```bash
35+
sudo apt update
36+
sudo apt install -y git zip unzip openjdk-17-jdk python3-pip autoconf libtool pkg-config zlib1g-dev libncurses5-dev libncursesw5-dev libtinfo5 cmake libffi-dev libssl-dev
37+
```
38+
39+
3. 初始化并构建 APK:
40+
```bash
41+
buildozer android debug
42+
```
43+
44+
4. APK 文件将生成在 `bin/` 目录下
45+
46+
### 方法二:使用 Python-for-Android
47+
48+
```bash
49+
pip install python-for-android
50+
p4a apk --private . --package=org.example.calculator --name "Calculator" --version 1.0 --bootstrap=sdl2 --requirements=python3,kivy --permission INTERNET
51+
```
52+
53+
### 方法三:使用在线服务
54+
55+
如果你在 Windows 上开发,可以使用以下在线服务:
56+
- **Google Colab** + Buildozer
57+
- **GitHub Actions** 自动构建
58+
59+
## 注意事项
60+
61+
- Buildozer 在 Linux 或 macOS 上运行最佳
62+
- Windows 用户建议使用 WSL2(Windows Subsystem for Linux)
63+
- 首次构建会下载 Android SDK/NDK,需要较长时间和稳定的网络连接
64+
- 确保有足够的磁盘空间(至少 10GB)
65+
66+
## 应用截图
67+
68+
计算器包含以下功能按钮:
69+
- 数字键:0-9
70+
- 运算符:+、-、*、/
71+
- 小数点:.
72+
- 清除:C
73+
- 等于:=

build_docker.bat

Whitespace-only changes.

build_windows.bat

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@echo off
2+
echo 正在安装 python-for-android...
3+
pip install python-for-android
4+
5+
echo.
6+
echo 开始构建 APK...
7+
p4a apk --private . --package=org.example.calculator --name "Calculator" --version 1.0 --bootstrap=sdl2 --requirements=python3,kivy --permission INTERNET --arch=armeabi-v7a
8+
9+
echo.
10+
echo 构建完成!APK 文件在当前目录下
11+
pause

buildozer.spec

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[app]
2+
title = Calculator
3+
package.name = calculator
4+
package.domain = org.example
5+
6+
source.dir = .
7+
source.include_exts = py,png,jpg,kv,atlas
8+
9+
version = 1.0
10+
11+
requirements = python3,kivy
12+
13+
orientation = portrait
14+
fullscreen = 0
15+
16+
android.permissions =
17+
android.api = 31
18+
android.minapi = 21
19+
android.ndk = 25b
20+
android.accept_sdk_license = True
21+
22+
[buildozer]
23+
log_level = 2
24+
warn_on_root = 1

main.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from kivy.app import App
2+
from kivy.uix.boxlayout import BoxLayout
3+
from kivy.uix.button import Button
4+
from kivy.uix.textinput import TextInput
5+
6+
7+
class CalculatorApp(App):
8+
def build(self):
9+
self.operators = ['+', '-', '*', '/']
10+
self.last_was_operator = None
11+
self.last_button = None
12+
13+
main_layout = BoxLayout(orientation='vertical')
14+
self.solution = TextInput(
15+
multiline=False, readonly=True, halign='right',
16+
font_size=55, size_hint=(1, 0.2)
17+
)
18+
main_layout.add_widget(self.solution)
19+
20+
buttons = [
21+
['7', '8', '9', '/'],
22+
['4', '5', '6', '*'],
23+
['1', '2', '3', '-'],
24+
['.', '0', 'C', '+'],
25+
]
26+
27+
for row in buttons:
28+
h_layout = BoxLayout()
29+
for label in row:
30+
button = Button(
31+
text=label,
32+
pos_hint={'center_x': 0.5, 'center_y': 0.5},
33+
font_size=32
34+
)
35+
button.bind(on_press=self.on_button_press)
36+
h_layout.add_widget(button)
37+
main_layout.add_widget(h_layout)
38+
39+
equals_button = Button(
40+
text='=', pos_hint={'center_x': 0.5, 'center_y': 0.5},
41+
font_size=32
42+
)
43+
equals_button.bind(on_press=self.on_solution)
44+
main_layout.add_widget(equals_button)
45+
46+
return main_layout
47+
48+
def on_button_press(self, instance):
49+
current = self.solution.text
50+
button_text = instance.text
51+
52+
if button_text == 'C':
53+
self.solution.text = ''
54+
else:
55+
if current and (
56+
self.last_was_operator and button_text in self.operators):
57+
return
58+
elif current == '' and button_text in self.operators:
59+
return
60+
else:
61+
new_text = current + button_text
62+
self.solution.text = new_text
63+
self.last_button = button_text
64+
self.last_was_operator = self.last_button in self.operators
65+
66+
def on_solution(self, instance):
67+
text = self.solution.text
68+
if text:
69+
try:
70+
solution = str(eval(self.solution.text))
71+
self.solution.text = solution
72+
except Exception:
73+
self.solution.text = 'Error'
74+
75+
76+
if __name__ == '__main__':
77+
CalculatorApp().run()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kivy==2.3.0

setup_windows.bat

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@echo off
2+
echo ========================================
3+
echo Python 计算器 - Windows 环境配置
4+
echo ========================================
5+
echo.
6+
7+
echo 步骤 1: 安装 Python 依赖
8+
pip install kivy python-for-android
9+
10+
echo.
11+
echo 步骤 2: 需要安装的额外工具
12+
echo.
13+
echo 请确保已安装以下工具:
14+
echo 1. Java JDK 8 或更高版本
15+
echo 下载地址: https://www.oracle.com/java/technologies/downloads/
16+
echo.
17+
echo 2. Android SDK(可通过 Android Studio 安装)
18+
echo 下载地址: https://developer.android.com/studio
19+
echo.
20+
echo 3. 设置环境变量:
21+
echo JAVA_HOME = JDK 安装路径
22+
echo ANDROID_HOME = Android SDK 路径
23+
echo PATH 中添加: %%ANDROID_HOME%%\tools 和 %%ANDROID_HOME%%\platform-tools
24+
echo.
25+
echo ========================================
26+
echo 配置完成后,运行 build_windows.bat 构建 APK
27+
echo ========================================
28+
pause

使用说明.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 使用 GitHub Actions 构建 Android APK
2+
3+
## 步骤 1:创建 GitHub 仓库
4+
5+
1. 访问 https://github.com/new
6+
2. 输入仓库名称,例如:`python-calculator-android`
7+
3. 选择 Public(公开)
8+
4. 点击 "Create repository"
9+
10+
## 步骤 2:上传代码到 GitHub
11+
12+
在当前目录打开命令行,执行以下命令:
13+
14+
```bash
15+
# 初始化 Git 仓库
16+
git init
17+
18+
# 添加所有文件
19+
git add .
20+
21+
# 提交
22+
git commit -m "Initial commit: Python Calculator"
23+
24+
# 添加远程仓库(替换成你的仓库地址)
25+
git remote add origin https://github.com/你的用户名/python-calculator-android.git
26+
27+
# 推送到 GitHub
28+
git push -u origin main
29+
```
30+
31+
如果推送时提示需要设置分支名称,先执行:
32+
```bash
33+
git branch -M main
34+
```
35+
36+
## 步骤 3:等待自动构建
37+
38+
1. 推送成功后,访问你的 GitHub 仓库
39+
2. 点击顶部的 "Actions" 标签
40+
3. 你会看到一个正在运行的工作流 "Build Android APK"
41+
4. 等待构建完成(大约 10-15 分钟)
42+
43+
## 步骤 4:下载 APK
44+
45+
1. 构建完成后,点击该工作流
46+
2. 在页面底部找到 "Artifacts" 部分
47+
3. 点击 "calculator-apk" 下载 ZIP 文件
48+
4. 解压后得到 APK 文件
49+
50+
## 步骤 5:安装到 Android 设备
51+
52+
1. 将 APK 文件传输到 Android 手机
53+
2. 在手机上打开文件管理器,找到 APK 文件
54+
3. 点击安装(可能需要允许"未知来源"的应用安装)
55+
4. 安装完成后即可使用
56+
57+
## 注意事项
58+
59+
- 首次构建可能需要较长时间
60+
- 如果构建失败,查看 Actions 日志找出原因
61+
- 每次推送代码都会自动触发构建
62+
- 也可以在 Actions 页面手动触发构建
63+
64+
## 如果遇到问题
65+
66+
1. 确保所有文件都已正确上传
67+
2. 检查 `.github/workflows/build.yml` 文件是否存在
68+
3. 查看 Actions 的构建日志
69+
4. 确保仓库是 Public(私有仓库可能有限制)

0 commit comments

Comments
 (0)