Although the python language is very versatile, it still seems a bit wrong to use it to develop apps. Therefore, apps developed with python should be used as coding exercises. In addition, the current modules in this area are not particularly mature and have many bugs.
Preparation work
Using python to develop apps requires the use of a module of python – kivy. kivy is an open source, cross-platform The platform's Python development framework for developing innovative applications. In short, this is a python desktop program development framework (similar to wxpython and other modules). The powerful thing is that kivy supports linux, mac, windows, android, and ios platforms. This is why this module is needed to develop apps.
Although kivy is cross-platform, if you want to use python code on different platforms, you also need to package the python code into an executable program for the corresponding platform. Fortunately, there is a packaging tool project under the kivy project – buildozer , this is the officially recommended packaging tool because it is relatively simple and has a high degree of automation. Other projects such as python-for-android can also play a similar role and will not be introduced here.
Building kivy development environment
You need to install the kivy development environment on your PC. Here is a demonstration of the installation process under mac and linux.
install kivy for mac
Install some dependent packages:
brew install pkg-config sdl2 sdl2_image sdl2_ttf sdl2_mixer gstreamer
Install cython and kivy:
pip install cython==0.25 pip install kivy
If an error occurs when installing kivy, use Install kivy in the following way:
git clone https://github.com/kivy/kivy python setup.py install
Post-installation test:
$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)]
Note: If there is no error when importing the kivy module, the installation is successful.
install kivy for centos7
First install the dependencies:
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
Install cython and kivy:
pip install Cython==0.20 pip install kivy
centos installation kivy reference: https://kivy.org/docs/installation/installation-linux .html#using-software-packages
Note: Other ways to install kivy can be found at: https://kivy.org/#download (FQ required)
Developed with kivy The first python app
After installing kivy, you can develop the app program. Here is a demonstration of the hello-world program. The more complex usage of kivy is not the focus of this article, and will be introduced in writing later.
1) Create a main.py file and write:
#! -*- coding:utf-8 -*- from kivy.app import App class HelloApp(App): pass if __name__ == '__main__': HelloApp().run()
2) Create a hello.kv file and write:
Label: text: 'Hello, World! I am nMask'
Simple description: main.py It is the entry function and defines a HelloApp class, which inherits kivy.app; the hello.kv file is a kivy program, which is equivalent to defining the interface style, etc. The naming rule of this file is that the class name is lowercase and app is removed.
Run the first python app
python main.py
Install the buildozer tool
Through the above coding, I created my own The first python app program. This program can run directly on mac, linux, and windows platforms. So how to make it run on Android or Apple phones? We know that to run on Android, it needs to be packaged into an apk installation program, so we need to use the buildozer tool mentioned earlier. (The buildozer tool can package kivy programs and supports android, ios, etc.). The installation process of buildozer is relatively simple. :
pip install buildozer
Use the buildozer tool to package the kivy program into an apk
Run it in the python project directory:
buildozer init
A successful run will create a configuration file buildozer.spec, which can Change the name of the app by modifying the configuration file, and then run:
buildozer android debug deploy run
Running the above command will generate a cross-platform installation package, applicable to Android, ios, etc. If used for Android, use python-for -android project.
When you run the above command for the first time, necessary files such as Android sdk will be automatically downloaded in the system.
The above is the detailed content of How to use python to develop mobile apps. For more information, please follow other related articles on the PHP Chinese website!