Python for Android Tutorial #3 – User Interface with Kivy Designer
This is the third post about mobile development with Python.
It’s a short tutorial, just to help you to understand and learn how to work with Python for Mobile.
In this post you will see how to create user interfaces with Kivy Designer. This post is compatible with any OS supported by Kivy.
III - About this tutorial
In this post you will learn about Kivy Designer.
This tool still in Alpha version, but it’s very helpful.
- Kivy 1.8+
- The following Python modules (available via pip)
- watchdog, http://pythonhosted.org/watchdog/
- The FileBrowser widget from the Kivy garden
To install the FileBrowser enter the console (on winodws use kivy.bat in the kivy folder):
With the perquisites installed, you can use the designer:
If you get any error in this step, feel free to comment here :)
IV - A Sample App
To test Kivy Designer, create a New Project and let’s use the TabbedPanel template.
This is your Kivy Designer:
First, if you want to learn more about it, make some changes in the KV Lang Area, and check how it modify your UI. You can learn more about KV Lang here. Next, open the Toolbox tab and add new widgets just to test.
Now, to start our project, in the Item 1, add two Buttons: ‘New Tab’ and ‘Close Tab’ inside the BoxLayout. You can remove the others tabs. In the events tab, edit the signal on_press and theses buttons.
In an event, you need to set the method name. First a prefix, app for a MainApp method, or root for a RootWidget method.
In this tutorial, I’ll create two callbacks, btn_new_on_press() and btn_close_on_press(), both of them in the RootWidget object.
Now we have the following KV code:
Now we will add some Python code.
When you click in the New Tab, a new Tab will be created with the same content of the first one. Close Tab will close the current tab.
Open your main.py file and add the methods in the RootWidget class:
Now save your project and check if it’s running.
V - Running on Android and iOS
Popen your terminal, and inside the project folder run the command bellow to create a buildozer project:
Now, open buildozer.spec and edit if necessary.
To run on Android, run the command:
Or, if you are going to run on iOS, run:
This is the app running on my Android device:
That’s all. Thanks for reading, if you have any problem or anything else, comment here :)