利用Python開發app需要用到Python的一個模組–kivy,kivy是一個開源的,跨平台的Python開發框架,用於開發使用創新的應用程式。簡而言之,這是一個Python桌面程式開發框架(類似wxpython等模組),強大的是kivy支援linux、mac、windows、android、ios平台,這也是為什麼開發app需要用到這個模組。
雖然kivy是跨平台的,但是想要在不同的平台使用Python程式碼,還需要將Python程式碼打包成對應平台的可執行程序,好在kivy專案下有個打包工具項目–buildozer,這是官方推薦的打包工具,因為相對比較簡單,自動化程度高,其他項目例如:Python-for-android也能起到類似的作用,這裡不展開介紹。
需要在pc上安裝kivy開發環境,這裡示範下mac與linux下的安裝流程。
install kivy for mac
安裝一些依賴套件:
brew install pkg-config sdl2 sdl2_image sdl2_ttf sdl2_mixer gstreamer
安裝cython以及kivy:
pip install cython==0.25 pip install kivy
如果安裝kivy報錯,則使用下面的方式安裝kivy:
git clone https://github.com/kivy/kivy python setup.py install
安裝後測試:
$python Python 2.7.10 (default, Jul 15 2017, 17:16:57) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> >>> import kivy [INFO ] [Logger] Record log in /Users/didi/.kivy/logs/kivy_18-05-08_4.txt [INFO ] [Kivy] v1.10.1.dev0, git-5f6c66e, 20180507 [INFO ] [Python] v2.7.10 (default, Jul 15 2017, 17:16:57) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
說明:導入kivy模組沒有報錯則說明安裝成功。
先安裝依賴:
yum install make mercurial automake gcc gcc-c++ SDL_ttf-devel SDL_mixer-devel khrplatform-devel mesa-libGLES mesa-libGLES-devel gstreamer-plugins-good gstreamer gstreamer-python mtdev-devel python-devel python-pip java-devel
安裝cython以及kivy:
pip install Cython==0.20 pip install kivy
安裝完kivy就可以開發app程式了,這裡示範下hello-world程序,關於kivy更複雜的用法不是本文重點,後面再成文介紹。
#! -*- coding:utf-8 -*- from kivy.app import App class HelloApp(App): pass if __name__ == '__main__': HelloApp().run()
Label: text: 'Hello, World! I am nMask'
簡單說明:main.py是入口函數,定義了一個HelloApp類,該類別繼承kivy.app;hello.kv檔案是kivy程序,相當於定義介面風格等,該檔案命名規則為類別名稱小寫且移除app。
python main.py
運行結果:
透過以上的編碼,我創建了自己的第一個python app程序,該程序可以直接在mac、linux、windows平台下運行,那麼如何讓它在安卓或蘋果手機上運行呢?我們知道在安卓上運行,需要將其打包成apk安裝程序,因此就需要用到前面提到過的buildozer工具,(buildozer工具可以打包kivy程序,支援android、ios等),buildozer的安裝過程比較簡單:
pip install buildozer
在python專案目錄下執行:
##
buildozer init
buildozer android debug deploy run
開啟app
Usage: buildozer [--profile] [--verbose] [target]... buildozer --version Available targets: androidAndroid target, based on python-for-android project iosiOS target, based on kivy-ios project android_oldAndroid target, based on python-for-android project (old toolchain) Global commands (without target): distcleanClean the whole Buildozer environment. help Show the Buildozer help. init Create a initial buildozer.spec in the current directory serveServe the bin directory via SimpleHTTPServer setdefault Set the default command to run when no arguments are given versionShow the Buildozer version Target commands: cleanClean the target environment update Update the target dependencies debugBuild the application in debug mode releaseBuild the application in release mode deploy Deploy the application on the device runRun the application on the device serveServe the bin directory via SimpleHTTPServer Target "android_old" commands: adbRun adb from the Android SDK. Args must come after --, or use --alias to make an alias logcat Show the log from the device Target "ios" commands: list_identitiesList the available identities to use for signing. xcodeOpen the xcode project. Target "android" commands: adbRun adb from the Android SDK. Args must come after --, or use --alias to make an alias logcat Show the log from the device p4aRun p4a commands. Args must come after --, or use --alias to make an alias
yum -y install --skip-broken glibc.i686 arts.i686 audiofile.i686 bzip2-libs.i686 cairo.i686 cyrus-sasl-lib.i686 dbus-libs.i686 directfb.i686 esound-libs.i686 fltk.i686 freeglut.i686 gtk2.i686 hal-libs.i686 imlib.i686 lcms-libs.i686 lesstif.i686 libacl.i686 libao.i686 libattr.i686 libcap.i686 libdrm.i686 libexif.i686 libgnomecanvas.i686 libICE.i686 libieee1284.i686 libsigc++20.i686 libSM.i686 libtool-ltdl.i686 libusb.i686 libwmf.i686 libwmf-lite.i686 libX11.i686 libXau.i686 libXaw.i686 libXcomposite.i686 libXdamage.i686 libXdmcp.i686 libXext.i686 libXfixes.i686 libxkbfile.i686 libxml2.i686 libXmu.i686 libXp.i686 libXpm.i686 libXScrnSaver.i686 libxslt.i686 libXt.i686 libXtst.i686 libXv.i686 libXxf86vm.i686 lzo.i686 mesa-libGL.i686 mesa-libGLU.i686 nas-libs.i686 nss_ldap.i686 cdk.i686 openldap.i686 pam.i686 popt.i686 pulseaudio-libs.i686 sane-backends-libs-gphoto2.i686 sane-backends-libs.i686 SDL.i686 svgalib.i686 unixODBC.i686 zlib.i686 compat-expat1.i686 compat-libstdc++-33.i686 openal-soft.i686 alsa-oss-libs.i686 redhat-lsb.i686 alsa-plugins-pulseaudio.i686 alsa-plugins-oss.i686 alsa-lib.i686 nspluginwrapper.i686 libXv.i686 libXScrnSaver.i686 qt.i686 qt-x11.i686 pulseaudio-libs.i686 pulseaudio-libs-glib2.i686 alsa-plugins-pulseaudio.i686 python-matplotli
pip install cython==0.25
在檔案開頭導入:
from distutils.version import LooseVersion
将786行:XXX found how the apk name is really built from the title这一行以下的代码替换为:
__sdk_dir = self.android_sdk_dir build_tools_versions = os.listdir(join(__sdk_dir, 'build-tools')) build_tools_versions = sorted(build_tools_versions, key=LooseVersion) build_tools_version = build_tools_versions[-1] gradle_files = ["build.gradle", "gradle", "gradlew"] is_gradle_build = any((exists(join(dist_dir, x)) for x in gradle_files)) and build_tools_version >= ’25.0'
kivy官方推出了一个buildozer虚拟机镜像,已经安装好了buildozer以及一些依赖文件,为buildozer打包测试提供平台。由于之前我在mac上利用buildozer打包一直报错,后来换成centos也依然没有成功,因此便下载了此虚拟机,测试效果如下:
说明:对于无法解决依赖问题的朋友,可以使用此虚拟机进行程序打包,开发环境还是推荐用自己的本机。
以上是怎麼利用Python開發App的詳細內容。更多資訊請關注PHP中文網其他相關文章!