pythonapp开发除了kivy还有什么_用Python开发Android App之Kivy初体验

Kivy简介:

Kivy是Python的跨平台GUI库,不仅支持windows,macOS ,linux,还支持android和iOS。凭这一点就非常吸引我,在了解Kivy之前我也了解了一下PyQt,相对来说,感觉PyQt打包apk还是略显复杂,所以选择了解进一步Kivy。

Kivy的核心思想是界面逻辑分离,kivy的kv文件控制界面显示部分,python控制逻辑部分。kivy现在在github上有9000+ star,并且还在继续更新维护,所以值得学习和了解一下。

Kivy安装:

安装这么复杂的库(处理各种依赖关系),我一般会直接使用anaconda,只需一个命令conda install kivy -c conda-forge.即可完成kivy安装,全程自动基本无需任何干预。当然如果你很嫌弃用anaconda 那么你就需要安装一系列工具包了,请在官网查阅相关命令,并做好心理准备处理各种依赖关系。

Kivy最简单的代码:

from kivy.app import Appfrom kivy.uix.button import Buttonclass TestApp(App): def build(self): return Button(text="Hello World")TestApp().run()

代码这里不进行解释了,如果您感兴趣去,建议阅读官网教程,或是参考一些这方面的书籍。

Kivy打包Apk:

最简单的方式就是下载非官方但是持续更新的VM,里面包括了所有打包相关的工具。只需调用一行命令就可以开始自动打包了。

Zen-CODE/kivybits​github.com

上述vm 是virtualbox的虚拟文件,要使用这么vm,你首先需要下载virtualbox。至于每一步的操作方式。我分享一篇英文文档给大家。这篇文档来自kivy官方社区。

How to package Kivy Apps for Android with Python 3.x in Windows 10?

Quick answer: you have to use an app called Buildozer running in Linux within a Virtual Machine installed in your Windows 10 computer. If this sounds a bit complicated, be glad that there are already a couple of Virtual Machines with all you need in them and even more such as several examples.

Below you have step-by-step information to go through this process and have your APK running in your Android device:

Set up the Kivy Buildozer Virtual Machine

1. Download and install the version of VirtualBox for your machine (for example Windows host for Windows 10) from:https://www.virtualbox.org/wiki/Downloads

2. Download and install the extension pack from (probably there are newer version, in such case update to the latest version):https://download.virtualbox.org/virtualbox/5.2.10/Oracle_VM_VirtualBox_Extension_Pack-5.2.10.vbox-extpack

3. Download Kivy Buildozer VM from:http://kivy.braintrainerplus.com/Kivy_Complete_VM_0.5.ova

4. Start the VirtualBox, click on File» Import Appliance...Select the downloaded file Kivy_Complete_VM_0.5.ova

5. Click on Settings» General» Advanced and select Bidirectional in the drop-down list for Shared Clipboard

6. Click on Settings» Shared Folders and click Adds new shared folder in a your Windows environment: for example C:\MyVirtual_Box (if you don’t have a folder for this, just create one in Windows) Click on Automount. You will use this folder to transfer your python/kivy project into the Virtual Machine and also to return the APK from the Virtual Machine to Windows.

7. Restart the Virtual Machine so the shared folder is mounted.

8. Done! You have your Virtual Machine ready to build your Android app.

Creating the APK for Android

1. Start your Virtual Box in Windows and start the environment you imported (called Kivy Complete VM) selecting it and clicking on the green arrow.

2. In Windows copy your folder with your project (for example: C:\myKivyProject\) into the Virtual Box. Important: your python file must be called main.py

3. In your Virtual Box double click File System icon (top left)

4. Click on sf_virtual_box, right click on your kivy Project (for example /myKivyProject as mentioned previously), the choose Copy

5. Click on Kivy (with the home icon) and then on >> Repos>>Python3>>kivy>>examples.

6. Right click and the folder and paste the folder with your project there.

7. Now go to >>home>>kivy>>Repos>>Python3>>kivy>>examples>>demo>>touchtracker and right click and copy the file called buildozer.spec

8. Go to your project folder copied earlier in >> Repos>>Python3>>kivy>>examples and paste the file copied in the previous step

9. Double click to edit the buildozer.spec file, and change app name, icon file and other parameters you consider. Save it on exit.

10. Right mouse click and select Open Terminal Here

11. At Terminal command prompt, type buildozer android debug

12. You have your APK in the Bin folder found in your current folder. Go there, right click on the APK and copy it into the share folder which is accessible from Windows (mentioned early as sf_virtual_box)

13. Close the Virtual Machine (as it may interfere in your USB connection to your Android device).

14. In Windows, and with your Android device plugged through USB, copy the APK from your shared folder in Windows (for example C:\MyVirtual_Box) to your device.

15. Now in your Android device, find and click, or touch, the APK and install it. You may found some warnings and confirm you trust in the developer :-)

16. The End! You should have your App installed in your Android device.

Notes:

There several steps that can be optimized and better configured, but if you are new to kivy/APK, this procedure is simple and practical.

To honor the geniuses behind all this magic, I’ve obtained most of the information from:

https://stackoverflow.com/questions/50126562/python-kivy-app-to-apk-on-windows-what-to-do-after-installing-a-virtual-machin/50145180#50145180

https://github.com/Zen-CODE/kivybits/tree/master/KivyCompleteVM

Document by Maxi Ichazo / version: 2019-04-16Kivy打包成功:

你可能感兴趣的:(pythonapp开发除了kivy还有什么_用Python开发Android App之Kivy初体验)