An Introduction to Kivy
A tutorial of sorts
Neil Muller
4 October 2013
Introduction
I Why?
Introduction
I Why?
I I want to write software that works on my phone
Introduction
I Why?
I I want to write software that works on my phone
I In general, I'd rather be writing python
Introduction
I Why?
I I want to write software that works on my phone
I In general, I'd rather be writing python
I What is kivy?
Introduction
I Why?
I I want to write software that works on my phone
I In general, I'd rather be writing python
I What is kivy?
I Kivy - Open source Python library for rapid development of
applications that make use of innovative user interfaces, such
as multi-touch apps. - www.kivy.org
Introduction
I Why?
I I want to write software that works on my phone
I In general, I'd rather be writing python
I What is kivy?
I Kivy - Open source Python library for rapid development of
applications that make use of innovative user interfaces, such
as multi-touch apps. - www.kivy.org
I Input stack is designed around touch interfaces
I Cross platform (Linux, MacOSX, Windows, Android, iOS)
I GPU accelerated (OpenGL ES 2)
Outline
I A very basic kivy application
I The kv language
I Properties & Events
I Widget Layout
I Dealing with media
I Conguration and other nifty stu
I Dealing with the hardware
I Deployment
A Very Basic Kivy App
I Hello World
A Very Basic Kivy App
I Hello World
I Important points
I Single window application
I Build returns root widget - single widget holding everything
else
I Most housekeeping tasks automatic, so focus is on the
application logic
A Very Basic Kivy App
I Hello World
I Important points
I Single window application
I Build returns root widget - single widget holding everything
else
I Most housekeeping tasks automatic, so focus is on the
application logic
I A more complex example
KV Language
I Describes the layout of the application
I More complicated logic lives in the python le
KV Language
I Describes the layout of the application
I More complicated logic lives in the python le
I Layout dened by a series of rules
I Each rule describes a widget tree
KV Language
I Describes the layout of the application
I More complicated logic lives in the python le
I Layout dened by a series of rules
I Each rule describes a widget tree
I Access to application (app), root widget of tree (root) and the
current widget (self )
I Can provide access to individual widgets if required
KV Language
I Describes the layout of the application
I More complicated logic lives in the python le
I Layout dened by a series of rules
I Each rule describes a widget tree
I Access to application (app), root widget of tree (root) and the
current widget (self )
I Can provide access to individual widgets if required
I Can also evaluate bits of code, so quite complex functionality
can live in here
I Can also play with the canvas here
Properties
I Provide Validation & Value / Bounds checking
Properties
I Provide Validation & Value / Bounds checking
I Can observe changes on the property
Properties
I Provide Validation & Value / Bounds checking
I Can observe changes on the property
I Needs to be associated with an EventDispatcher for this to
work
I Fortunately, pretty much everything of interest in kivy is a
subclass of EventDispatcher
Properties
I Provide Validation & Value / Bounds checking
I Can observe changes on the property
I Needs to be associated with an EventDispatcher for this to
work
I Fortunately, pretty much everything of interest in kivy is a
subclass of EventDispatcher
I Useful set of default properties
I StringProperty, NumericProperty, BoundedNumericProperty,
OptionProperty, etc.
I Easy to create custom properties
Events
I Basic event is MotionEvent
I Although TouchEvent subclass is what we're almost always
want
Events
I Basic event is MotionEvent
I Although TouchEvent subclass is what we're almost always
want
I Can identify source and extra information from event prole
I Handlers can be added either via subclassing, or via bind
Events
I Basic event is MotionEvent
I Although TouchEvent subclass is what we're almost always
want
I Can identify source and extra information from event prole
I Handlers can be added either via subclassing, or via bind
I NB: Touch events are not limited to only the widgets they
overlap with
I Handle this logic yourself if it's the right thing to do
Events
I Basic event is MotionEvent
I Although TouchEvent subclass is what we're almost always
want
I Can identify source and extra information from event prole
I Handlers can be added either via subclassing, or via bind
I NB: Touch events are not limited to only the widgets they
overlap with
I Handle this logic yourself if it's the right thing to do
I Expected set of widget created events
I on_press for buttons, etc.
Widget Layout
I By default, layout is global to the root window
I No automatic management of widget layout
I Need to respond to scroll or scale events manually
Widget Layout
I By default, layout is global to the root window
I No automatic management of widget layout
I Need to respond to scroll or scale events manually
I Layout's provide widget containers which can handle some of
this
I Allow the use of relative positioning (pos_hint & size_hint)
Widget Layout
I By default, layout is global to the root window
I No automatic management of widget layout
I Need to respond to scroll or scale events manually
I Layout's provide widget containers which can handle some of
this
I Allow the use of relative positioning (pos_hint & size_hint)
I Large number of layout's available
I BoxLayout - arranges widgets horizontally or vertically
I FloatLayout - Free positioning with layout advantages
I GridLayout - what it says on the tin
I RelativeLayout - position stays relative to the layout, which
simplies scrolling
I And so on
Media
I Kivy provides easy image loading and manipulation functions
I Also support for sound & video
Media
I Kivy provides easy image loading and manipulation functions
I Also support for sound & video
I Very useful cache implementation as well
Media
I Kivy provides easy image loading and manipulation functions
I Also support for sound & video
I Very useful cache implementation as well
I For many cases, you dont want to do this by hand
I Especially for images, look at kivy's atlas
Conguration and other nifty stu
I Conguration system is based o CongParser
I Nifty mostly automatic cong screen setup
Conguration and other nifty stu
I Conguration system is based o CongParser
I Nifty mostly automatic cong screen setup
I Power management events can be handled by watching
application events
I on_pause / on_resume
Conguration and other nifty stu
I Conguration system is based o CongParser
I Nifty mostly automatic cong screen setup
I Power management events can be handled by watching
application events
I on_pause / on_resume
I NB: current default pause behaviour to stop the application
I on_resume can require some care, especially for GL heavy apps
Conguration and other nifty stu
I Conguration system is based o CongParser
I Nifty mostly automatic cong screen setup
I Power management events can be handled by watching
application events
I on_pause / on_resume
I NB: current default pause behaviour to stop the application
I on_resume can require some care, especially for GL heavy apps
I Other useful application events - on_stop, on_start,
on_cong_changed
Dealing with hardware
I Current state of play
I pyjnius provides access to the native interfaces for Android
I hw = jnius.autoclass('org.renpy.android.Hardware')
I Still need to know all the java interface details
I Things work dierently on other platforms (pyobjus for iOS,
etc)
Dealing with hardware
I Current state of play
I pyjnius provides access to the native interfaces for Android
I hw = jnius.autoclass('org.renpy.android.Hardware')
I Still need to know all the java interface details
I Things work dierently on other platforms (pyobjus for iOS,
etc)
I The better future (still a work in progress)
Dealing with hardware
I Current state of play
I pyjnius provides access to the native interfaces for Android
I hw = jnius.autoclass('org.renpy.android.Hardware')
I Still need to know all the java interface details
I Things work dierently on other platforms (pyobjus for iOS,
etc)
I The better future (still a work in progress)
I plyer
I Abstraction layer over common functionality
I Current aim is to support Camera, Accelerometer,
Notications and Text to Speech
I Under reasonably active development - android support
reasonably eshed out, but other platforms need work
Deployment to Android
I For ddling with kivy, kivy-launcher is great
I But not a genuine deployment option
Deployment to Android
I For ddling with kivy, kivy-launcher is great
I But not a genuine deployment option
I Need to compile android-for-python to generate an apk
I Lots of documentation and howtos
I Still all a bit tedious, though
Deployment to Android
I For ddling with kivy, kivy-launcher is great
I But not a genuine deployment option
I Need to compile android-for-python to generate an apk
I Lots of documentation and howtos
I Still all a bit tedious, though
I Recently started project - buildozer
I Unies a lot of the grunt work
I Everything handled by a single spec le
I Handles android and iOS targets
Deployment to Android
I For ddling with kivy, kivy-launcher is great
I But not a genuine deployment option
I Need to compile android-for-python to generate an apk
I Lots of documentation and howtos
I Still all a bit tedious, though
I Recently started project - buildozer
I Unies a lot of the grunt work
I Everything handled by a single spec le
I Handles android and iOS targets
I Will download missing components if needed when building
the apk
I i.e. use with great care when behind a slow network connection