kivy tutorial for beginner's (python) Part 1

                                            image from kivy.org


Start Developing Kivy Apps Right Away!

Creating Kivy apps is fun and rewarding. This guide should be the perfect starting point to get you on the right track for app development. You will require a basic knowledge of Python to follow this introduction.

Using Kivy on your computer, you can create apps that run on:

  • Desktop computers: OS X, Linux, Windows.
  • iOS devices: iPad, iPhone.
  • Android devices: tablets, phones.
  • Any other touch-enabled professional/homebrew devices supporting TUIO (Tangible User Interface Objects).

                                            #INSTALLATION
 If you want the development version of Kivy in order to benefit from the latest additions to the framework, you can get the source code from github:
                  git clone http://github.com/kivy/kivy
 installation on windows :

Using Conda |

conda install kivy -c conda-forge

Otherwise, continue below to install Kivy in a native Python installation.

Installing Kivy Stable Release

  1. Ensure you have the latest pip, wheel, and virtualenv:
    python -m pip install --upgrade pip wheel setuptools virtualenv 
    Optionally create a new virtual environment for your Kivy project. Highly recommended:
    1. First create the environment named kivy_venv in your current directory:
      python -m virtualenv kivy_venv 
    2. Activate the virtual environment. You’ll have to do this step from the current directory every time you start a new terminal. On windows CMD do:
      kivy_venv\Scripts\activate
      If you’re in a bash terminal, instead do
      source kivy_venv/Scripts/activate
    Your terminal should now preface the path with something like (kivy_venv), indicating that the kivy_venv environment is active. If it doesn’t say that, the virtual environment is not active.
  2. Install the dependencies (skip gstreamer (~120MB) if not needed, see Kivy’s dependencies). If you are upgrading Kivy, see Updating Kivy from a previous release:
    python -m pip install docutils pygments pypiwin32 kivy_deps.sdl2==0.1.* kivy_deps.glew==0.1.*
    python -m pip install kivy_deps.gstreamer==0.1.*
    
    Note
    If you encounter a MemoryError while installing, add after pip install the –no-cache-dir option.
    For Python 3.5+, you can also use the angle backend instead of glew. This can be installed with:
    python -m pip install kivy_deps.angle==0.1.* 
    Warning
    When installing, pin kivy’s dependencies to the specific version that was released on pypi when your kivy version was released, like above. Otherwise you may get an incompatible dependency when it is updated in the future.
  3. Install kivy:
    python -m pip install kivy==1.11.1 
  4. (Optionally) Install the kivy examples:
    python -m pip install kivy_examples==1.11.1 
    The examples are installed in the share directory under the root directory where python is installed.
That’s it. You should now be able to import kivy in python or run a basic example if you installed the kivy examples:
python kivy_venv\share\kivy-examples\demo\showcase\main.py
Replace kivy_venv with the path where python is installed if you didn’t use a virtualenv.
Note
If you encounter any permission denied errors, try opening the Command prompt as administrator and trying again. The best solution for this is to use a virtual environment instead.

  - First Kivy App

In this section, you will learn how to create and run a Kivy program and how to build a basic interface in Kivy.

Let’s create a file with .py extension.

To create a Kivy interface, we first need to import the Kivy app module in our program using the following statement:

from kivy.app import App
Now importing Label from kivy.uix.label:
from kivy.uix.label import Label
Now is the time to write our main program.
class FirstKivy(App):

    def build(self):

        return Label(text="Hello Kivy!")
In the above snippet, a class is inherited from the App class. Then to build the application we have to return a widget on the build() function. In the code above, we have returned a label with text “Hello Kivy”.

The last step is to call this function. You can either create an object of the class or just write the following statement:
FirstKivy().run()
The entire python file looks like the following:
from kivy.app import App

from kivy.uix.label import Label

class FirstKivy(App):

    def build(self):

        return Label(text="Hello Kivy!")

FirstKivy().run()
The result of this code will be like this:


 Part two of kivy tutorial coming soon...

    Choose :
  • OR
  • To comment
No comments:
Write Comments