An Introduction to Kivy
A tutorial of sorts
Neil Muller
4 October 2013
Introduction
I Why?
I I want to write software that works on my phoneI In general, I'd rather be writing python
I What is kivy?
I �Kivy - Open source Python library for rapid development ofapplications that make use of innovative user interfaces, suchas multi-touch apps.� - www.kivy.org
I Input stack is designed around touch interfacesI Cross platform (Linux, MacOSX, Windows, Android, iOS)I GPU accelerated (OpenGL ES 2)
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 ofapplications that make use of innovative user interfaces, suchas multi-touch apps.� - www.kivy.org
I Input stack is designed around touch interfacesI Cross platform (Linux, MacOSX, Windows, Android, iOS)I GPU accelerated (OpenGL ES 2)
Introduction
I Why?
I I want to write software that works on my phoneI In general, I'd rather be writing python
I What is kivy?
I �Kivy - Open source Python library for rapid development ofapplications that make use of innovative user interfaces, suchas multi-touch apps.� - www.kivy.org
I Input stack is designed around touch interfacesI Cross platform (Linux, MacOSX, Windows, Android, iOS)I GPU accelerated (OpenGL ES 2)
Introduction
I Why?
I I want to write software that works on my phoneI In general, I'd rather be writing python
I What is kivy?
I �Kivy - Open source Python library for rapid development ofapplications that make use of innovative user interfaces, suchas multi-touch apps.� - www.kivy.org
I Input stack is designed around touch interfacesI Cross platform (Linux, MacOSX, Windows, Android, iOS)I GPU accelerated (OpenGL ES 2)
Introduction
I Why?
I I want to write software that works on my phoneI In general, I'd rather be writing python
I What is kivy?
I �Kivy - Open source Python library for rapid development ofapplications that make use of innovative user interfaces, suchas multi-touch apps.� - www.kivy.org
I Input stack is designed around touch interfacesI Cross platform (Linux, MacOSX, Windows, Android, iOS)I GPU accelerated (OpenGL ES 2)
Introduction
I Why?
I I want to write software that works on my phoneI In general, I'd rather be writing python
I What is kivy?
I �Kivy - Open source Python library for rapid development ofapplications that make use of innovative user interfaces, suchas multi-touch apps.� - www.kivy.org
I Input stack is designed around touch interfacesI 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 Con�guration and other nifty stu�
I Dealing with the hardware
I Deployment
A Very Basic Kivy App
I Hello World
I Important points
I Single window applicationI Build returns �root widget� - single widget holding everything
elseI Most housekeeping tasks automatic, so focus is on the
application logic
I A more complex example
A Very Basic Kivy App
I Hello World
I Important points
I Single window applicationI Build returns �root widget� - single widget holding everything
elseI Most housekeeping tasks automatic, so focus is on the
application logic
I A more complex example
A Very Basic Kivy App
I Hello World
I Important points
I Single window applicationI Build returns �root widget� - single widget holding everything
elseI 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
I Layout de�ned by a series of rules
I Each rule describes a widget tree
I Access to application (app), root widget of tree (root) and thecurrent widget (self)
I Can provide access to individual widgets if required
I Can also evaluate bits of code, so quite complex functionalitycan live in here
I Can also play with the canvas here
KV Language
I Describes the layout of the application
I More complicated logic lives in the python �le
I Layout de�ned by a series of rules
I Each rule describes a widget tree
I Access to application (app), root widget of tree (root) and thecurrent widget (self)
I Can provide access to individual widgets if required
I Can also evaluate bits of code, so quite complex functionalitycan live in here
I Can also play with the canvas here
KV Language
I Describes the layout of the application
I More complicated logic lives in the python �le
I Layout de�ned by a series of rules
I Each rule describes a widget tree
I Access to application (app), root widget of tree (root) and thecurrent widget (self)
I Can provide access to individual widgets if required
I Can also evaluate bits of code, so quite complex functionalitycan live in here
I Can also play with the canvas here
KV Language
I Describes the layout of the application
I More complicated logic lives in the python �le
I Layout de�ned by a series of rules
I Each rule describes a widget tree
I Access to application (app), root widget of tree (root) and thecurrent widget (self)
I Can provide access to individual widgets if required
I Can also evaluate bits of code, so quite complex functionalitycan live in here
I Can also play with the canvas here
Properties
I Provide Validation & Value / Bounds checking
I Can observe changes on the property
I Needs to be associated with an EventDispatcher for this towork
I Fortunately, pretty much everything of interest in kivy is asubclass of EventDispatcher
I Useful set of default properties
I StringProperty, NumericProperty, BoundedNumericProperty,OptionProperty, etc.
I Easy to create custom properties
Properties
I Provide Validation & Value / Bounds checking
I Can observe changes on the property
I Needs to be associated with an EventDispatcher for this towork
I Fortunately, pretty much everything of interest in kivy is asubclass of EventDispatcher
I Useful set of default properties
I StringProperty, NumericProperty, BoundedNumericProperty,OptionProperty, etc.
I Easy to create custom properties
Properties
I Provide Validation & Value / Bounds checking
I Can observe changes on the property
I Needs to be associated with an EventDispatcher for this towork
I Fortunately, pretty much everything of interest in kivy is asubclass of EventDispatcher
I Useful set of default properties
I StringProperty, NumericProperty, BoundedNumericProperty,OptionProperty, etc.
I Easy to create custom properties
Properties
I Provide Validation & Value / Bounds checking
I Can observe changes on the property
I Needs to be associated with an EventDispatcher for this towork
I Fortunately, pretty much everything of interest in kivy is asubclass 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 alwayswant
I Can identify source and extra information from event pro�leI Handlers can be added either via subclassing, or via bind
I NB: Touch events are not limited to only the widgets theyoverlap 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.
Events
I Basic event is MotionEvent
I Although TouchEvent subclass is what we're almost alwayswant
I Can identify source and extra information from event pro�leI Handlers can be added either via subclassing, or via bind
I NB: Touch events are not limited to only the widgets theyoverlap 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.
Events
I Basic event is MotionEvent
I Although TouchEvent subclass is what we're almost alwayswant
I Can identify source and extra information from event pro�leI Handlers can be added either via subclassing, or via bind
I NB: Touch events are not limited to only the widgets theyoverlap 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.
Events
I Basic event is MotionEvent
I Although TouchEvent subclass is what we're almost alwayswant
I Can identify source and extra information from event pro�leI Handlers can be added either via subclassing, or via bind
I NB: Touch events are not limited to only the widgets theyoverlap 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
I Layout's provide widget containers which can handle some ofthis
I Allow the use of relative positioning (pos_hint & size_hint)
I Large number of layout's available
I BoxLayout - arranges widgets horizontally or verticallyI FloatLayout - Free positioning with layout advantagesI GridLayout - what it says on the tinI RelativeLayout - position stays relative to the layout, which
simpli�es scrollingI And so on
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 ofthis
I Allow the use of relative positioning (pos_hint & size_hint)
I Large number of layout's available
I BoxLayout - arranges widgets horizontally or verticallyI FloatLayout - Free positioning with layout advantagesI GridLayout - what it says on the tinI RelativeLayout - position stays relative to the layout, which
simpli�es scrollingI And so on
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 ofthis
I Allow the use of relative positioning (pos_hint & size_hint)
I Large number of layout's available
I BoxLayout - arranges widgets horizontally or verticallyI FloatLayout - Free positioning with layout advantagesI GridLayout - what it says on the tinI RelativeLayout - position stays relative to the layout, which
simpli�es scrollingI And so on
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
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
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
Con�guration and other nifty stu�
I Con�guration system is based o� Con�gParser
I Nifty mostly automatic con�g screen setup
I Power management events can be handled by watchingapplication events
I on_pause / on_resume
I NB: current default pause behaviour to stop the applicationI on_resume can require some care, especially for GL heavy apps
I Other useful application events - on_stop, on_start,on_con�g_changed
Con�guration and other nifty stu�
I Con�guration system is based o� Con�gParser
I Nifty mostly automatic con�g screen setup
I Power management events can be handled by watchingapplication events
I on_pause / on_resume
I NB: current default pause behaviour to stop the applicationI on_resume can require some care, especially for GL heavy apps
I Other useful application events - on_stop, on_start,on_con�g_changed
Con�guration and other nifty stu�
I Con�guration system is based o� Con�gParser
I Nifty mostly automatic con�g screen setup
I Power management events can be handled by watchingapplication events
I on_pause / on_resume
I NB: current default pause behaviour to stop the applicationI on_resume can require some care, especially for GL heavy apps
I Other useful application events - on_stop, on_start,on_con�g_changed
Con�guration and other nifty stu�
I Con�guration system is based o� Con�gParser
I Nifty mostly automatic con�g screen setup
I Power management events can be handled by watchingapplication events
I on_pause / on_resume
I NB: current default pause behaviour to stop the applicationI on_resume can require some care, especially for GL heavy apps
I Other useful application events - on_stop, on_start,on_con�g_changed
Dealing with hardware
I Current state of play
I pyjnius provides access to the native interfaces for AndroidI hw = jnius.autoclass('org.renpy.android.Hardware')I Still need to know all the java interface detailsI Things work di�erently on other platforms (pyobjus for iOS,
etc)
I The better future (still a work in progress)
I plyerI Abstraction layer over common functionalityI Current aim is to support Camera, Accelerometer,
Noti�cations and Text to SpeechI Under reasonably active development - android support
reasonably �eshed out, but other platforms need work
Dealing with hardware
I Current state of play
I pyjnius provides access to the native interfaces for AndroidI hw = jnius.autoclass('org.renpy.android.Hardware')I Still need to know all the java interface detailsI Things work di�erently on other platforms (pyobjus for iOS,
etc)
I The better future (still a work in progress)
I plyerI Abstraction layer over common functionalityI Current aim is to support Camera, Accelerometer,
Noti�cations and Text to SpeechI Under reasonably active development - android support
reasonably �eshed out, but other platforms need work
Dealing with hardware
I Current state of play
I pyjnius provides access to the native interfaces for AndroidI hw = jnius.autoclass('org.renpy.android.Hardware')I Still need to know all the java interface detailsI Things work di�erently on other platforms (pyobjus for iOS,
etc)
I The better future (still a work in progress)
I plyerI Abstraction layer over common functionalityI Current aim is to support Camera, Accelerometer,
Noti�cations and Text to SpeechI 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
I Need to compile android-for-python to generate an apk
I Lots of documentation and howtosI Still all a bit tedious, though
I Recently started project - buildozer
I Uni�es a lot of the grunt workI Everything handled by a single spec �leI Handles android and iOS targets
I Will download missing components if needed when buildingthe apk
I i.e. use with great care when behind a slow network connection
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 howtosI Still all a bit tedious, though
I Recently started project - buildozer
I Uni�es a lot of the grunt workI Everything handled by a single spec �leI Handles android and iOS targets
I Will download missing components if needed when buildingthe apk
I i.e. use with great care when behind a slow network connection
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 howtosI Still all a bit tedious, though
I Recently started project - buildozer
I Uni�es a lot of the grunt workI Everything handled by a single spec �leI Handles android and iOS targets
I Will download missing components if needed when buildingthe apk
I i.e. use with great care when behind a slow network connection
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 howtosI Still all a bit tedious, though
I Recently started project - buildozer
I Uni�es a lot of the grunt workI Everything handled by a single spec �leI Handles android and iOS targets
I Will download missing components if needed when buildingthe apk
I i.e. use with great care when behind a slow network connection