2010-12-21 01:13:06 +00:00
|
|
|
.. _quickstart:
|
|
|
|
|
|
|
|
Quickstart
|
|
|
|
==========
|
|
|
|
|
2011-01-02 16:34:18 +00:00
|
|
|
This page explains how to create a simple Kivy *"Hello world"* program.
|
|
|
|
This assumes you already have Kivy installed. If you do not, head over to the
|
|
|
|
:ref:`installation` section. We also assume basic `Python <http://docs.python.org/tutorial/>`_
|
2011-03-17 19:47:51 +00:00
|
|
|
2.x knowledge throughout the rest of this documentation.
|
2010-12-21 01:13:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
Create an application
|
|
|
|
---------------------
|
|
|
|
|
2011-01-02 16:34:18 +00:00
|
|
|
The base code for creating an application looks like this:
|
2010-12-21 01:13:06 +00:00
|
|
|
|
|
|
|
.. sourcecode:: python
|
|
|
|
|
2011-02-06 22:00:08 +00:00
|
|
|
import kivy
|
2011-05-29 18:19:29 +00:00
|
|
|
kivy.require('1.0.6') # replace with your current kivy version !
|
2011-02-06 22:00:08 +00:00
|
|
|
|
2010-12-21 01:13:06 +00:00
|
|
|
from kivy.app import App
|
|
|
|
from kivy.uix.button import Button
|
|
|
|
|
|
|
|
class MyApp(App):
|
|
|
|
def build(self):
|
2011-01-30 12:57:36 +00:00
|
|
|
return Button(text='Hello World')
|
2010-12-21 01:13:06 +00:00
|
|
|
|
2011-04-22 14:24:47 +00:00
|
|
|
if __name__ in ('__android__', '__main__'):
|
|
|
|
MyApp().run()
|
2010-12-21 01:13:06 +00:00
|
|
|
|
2011-02-06 22:00:08 +00:00
|
|
|
Save it as `main.py`.
|
2010-12-21 01:13:06 +00:00
|
|
|
|
2011-03-17 19:47:51 +00:00
|
|
|
To run the application, follow the instructions for your operating system:
|
2011-02-03 15:02:23 +00:00
|
|
|
|
|
|
|
Linux
|
|
|
|
Follow the instructions for :ref:`running Kivy application on Linux <linux-run-app>`::
|
|
|
|
|
2011-02-06 22:00:08 +00:00
|
|
|
$ python main.py
|
2011-02-03 15:02:23 +00:00
|
|
|
|
|
|
|
Windows
|
|
|
|
Follow the instructions for :ref:`running Kivy application on Windows <windows-run-app>`::
|
2011-10-30 22:15:46 +00:00
|
|
|
|
2011-02-06 22:00:08 +00:00
|
|
|
$ python main.py
|
2011-02-03 15:02:23 +00:00
|
|
|
# or
|
2011-02-06 22:00:08 +00:00
|
|
|
C:\appdir>kivy.bat main.py
|
2011-02-03 15:02:23 +00:00
|
|
|
|
2011-03-16 17:19:57 +00:00
|
|
|
Mac OS X
|
2011-02-03 15:02:23 +00:00
|
|
|
Follow the instructions for :ref:`running Kivy application on MacOSX <macosx-run-app>`::
|
|
|
|
|
2011-02-06 22:00:08 +00:00
|
|
|
$ kivy main.py
|
2011-02-03 15:02:23 +00:00
|
|
|
|
|
|
|
Android
|
2011-03-16 17:19:57 +00:00
|
|
|
Your application needs some complementary files to be able to run on Android.
|
|
|
|
See :doc:`android` for further reference.
|
2010-12-21 01:13:06 +00:00
|
|
|
|
2011-01-10 19:51:38 +00:00
|
|
|
A window should open, showing a sole button (with the label 'Hello World') that
|
|
|
|
covers the entire window's area. That's all there is to it.
|
2010-12-21 01:13:06 +00:00
|
|
|
|
2011-06-06 14:30:39 +00:00
|
|
|
.. image:: images/quickstart.jpg
|
|
|
|
:align: center
|
|
|
|
|
2011-01-30 13:59:58 +00:00
|
|
|
So what does that code do?
|
2010-12-21 01:13:06 +00:00
|
|
|
|
2011-02-06 22:00:08 +00:00
|
|
|
#. First, we import Kivy, and check if the current installed version will be
|
|
|
|
enough for our application. If not, an exception will be automatically
|
|
|
|
fired, and prevent your application to crash in runtime. You can read the
|
|
|
|
documentation of :func:`kivy.require` function for more information.
|
|
|
|
#. We import the :class:`~kivy.app.App` class, to be able to subclass it.
|
2011-01-30 13:59:58 +00:00
|
|
|
By subclassing this class, your own class gains several features that
|
2011-01-02 16:34:18 +00:00
|
|
|
we already developed for you to make sure it will be recognized by
|
|
|
|
Kivy.
|
2011-01-30 13:59:58 +00:00
|
|
|
#. Next, we import the :class:`~kivy.uix.button.Button` class, to be able to
|
2010-12-21 01:13:06 +00:00
|
|
|
create an instance of a button with a custom label.
|
2011-01-30 13:59:58 +00:00
|
|
|
#. Then, we create our application class, based on the App class.
|
2011-01-02 16:34:18 +00:00
|
|
|
We extend the :meth:`~kivy.app.App.build` function to be able to return an
|
2010-12-21 01:13:06 +00:00
|
|
|
instance of :class:`~kivy.uix.button.Button`. This instance will be used
|
2011-01-30 13:59:58 +00:00
|
|
|
as the root of the widget tree (because we returned it).
|
|
|
|
#. Finally, we call :meth:`~kivy.app.App.run` on our application instance to
|
2011-01-02 16:34:18 +00:00
|
|
|
launch the Kivy process with our application inside.
|
2010-12-21 01:13:06 +00:00
|
|
|
|