Slide 1

Slide 1 text

An Introduction to Kivy A tutorial of sorts Neil Muller 4 October 2013

Slide 2

Slide 2 text

Introduction Why?

Slide 3

Slide 3 text

Introduction Why? I want to write software that works on my phone

Slide 4

Slide 4 text

Introduction Why? I want to write software that works on my phone In general, I'd rather be writing python

Slide 5

Slide 5 text

Introduction Why? I want to write software that works on my phone In general, I'd rather be writing python What is kivy?

Slide 6

Slide 6 text

Introduction Why? I want to write software that works on my phone In general, I'd rather be writing python What is kivy? 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

Slide 7

Slide 7 text

Introduction Why? I want to write software that works on my phone In general, I'd rather be writing python What is kivy? 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 Input stack is designed around touch interfaces Cross platform (Linux, MacOSX, Windows, Android, iOS) GPU accelerated (OpenGL ES 2)

Slide 8

Slide 8 text

Outline A very basic kivy application The kv language Properties & Events Widget Layout Dealing with media Conguration and other nifty stu Dealing with the hardware Deployment

Slide 9

Slide 9 text

A Very Basic Kivy App Hello World

Slide 10

Slide 10 text

A Very Basic Kivy App Hello World Important points Single window application Build returns root widget - single widget holding everything else Most housekeeping tasks automatic, so focus is on the application logic

Slide 11

Slide 11 text

A Very Basic Kivy App Hello World Important points Single window application Build returns root widget - single widget holding everything else Most housekeeping tasks automatic, so focus is on the application logic A more complex example

Slide 12

Slide 12 text

KV Language Describes the layout of the application More complicated logic lives in the python le

Slide 13

Slide 13 text

KV Language Describes the layout of the application More complicated logic lives in the python le Layout dened by a series of rules Each rule describes a widget tree

Slide 14

Slide 14 text

KV Language Describes the layout of the application More complicated logic lives in the python le Layout dened by a series of rules Each rule describes a widget tree Access to application (app), root widget of tree (root) and the current widget (self) Can provide access to individual widgets if required

Slide 15

Slide 15 text

KV Language Describes the layout of the application More complicated logic lives in the python le Layout dened by a series of rules Each rule describes a widget tree Access to application (app), root widget of tree (root) and the current widget (self) Can provide access to individual widgets if required Can also evaluate bits of code, so quite complex functionality can live in here Can also play with the canvas here

Slide 16

Slide 16 text

Properties Provide Validation & Value / Bounds checking

Slide 17

Slide 17 text

Properties Provide Validation & Value / Bounds checking Can observe changes on the property

Slide 18

Slide 18 text

Properties Provide Validation & Value / Bounds checking Can observe changes on the property Needs to be associated with an EventDispatcher for this to work Fortunately, pretty much everything of interest in kivy is a subclass of EventDispatcher

Slide 19

Slide 19 text

Properties Provide Validation & Value / Bounds checking Can observe changes on the property Needs to be associated with an EventDispatcher for this to work Fortunately, pretty much everything of interest in kivy is a subclass of EventDispatcher Useful set of default properties StringProperty, NumericProperty, BoundedNumericProperty, OptionProperty, etc. Easy to create custom properties

Slide 20

Slide 20 text

Events Basic event is MotionEvent Although TouchEvent subclass is what we're almost always want

Slide 21

Slide 21 text

Events Basic event is MotionEvent Although TouchEvent subclass is what we're almost always want Can identify source and extra information from event prole Handlers can be added either via subclassing, or via bind

Slide 22

Slide 22 text

Events Basic event is MotionEvent Although TouchEvent subclass is what we're almost always want Can identify source and extra information from event prole Handlers can be added either via subclassing, or via bind NB: Touch events are not limited to only the widgets they overlap with Handle this logic yourself if it's the right thing to do

Slide 23

Slide 23 text

Events Basic event is MotionEvent Although TouchEvent subclass is what we're almost always want Can identify source and extra information from event prole Handlers can be added either via subclassing, or via bind NB: Touch events are not limited to only the widgets they overlap with Handle this logic yourself if it's the right thing to do Expected set of widget created events on_press for buttons, etc.

Slide 24

Slide 24 text

Widget Layout By default, layout is global to the root window No automatic management of widget layout Need to respond to scroll or scale events manually

Slide 25

Slide 25 text

Widget Layout By default, layout is global to the root window No automatic management of widget layout Need to respond to scroll or scale events manually Layout's provide widget containers which can handle some of this Allow the use of relative positioning (pos_hint & size_hint)

Slide 26

Slide 26 text

Widget Layout By default, layout is global to the root window No automatic management of widget layout Need to respond to scroll or scale events manually Layout's provide widget containers which can handle some of this Allow the use of relative positioning (pos_hint & size_hint) Large number of layout's available BoxLayout - arranges widgets horizontally or vertically FloatLayout - Free positioning with layout advantages GridLayout - what it says on the tin RelativeLayout - position stays relative to the layout, which simplies scrolling And so on

Slide 27

Slide 27 text

Media Kivy provides easy image loading and manipulation functions Also support for sound & video

Slide 28

Slide 28 text

Media Kivy provides easy image loading and manipulation functions Also support for sound & video Very useful cache implementation as well

Slide 29

Slide 29 text

Media Kivy provides easy image loading and manipulation functions Also support for sound & video Very useful cache implementation as well For many cases, you dont want to do this by hand Especially for images, look at kivy's atlas

Slide 30

Slide 30 text

Conguration and other nifty stu Conguration system is based o CongParser Nifty mostly automatic cong screen setup

Slide 31

Slide 31 text

Conguration and other nifty stu Conguration system is based o CongParser Nifty mostly automatic cong screen setup Power management events can be handled by watching application events on_pause / on_resume

Slide 32

Slide 32 text

Conguration and other nifty stu Conguration system is based o CongParser Nifty mostly automatic cong screen setup Power management events can be handled by watching application events on_pause / on_resume NB: current default pause behaviour to stop the application on_resume can require some care, especially for GL heavy apps

Slide 33

Slide 33 text

Conguration and other nifty stu Conguration system is based o CongParser Nifty mostly automatic cong screen setup Power management events can be handled by watching application events on_pause / on_resume NB: current default pause behaviour to stop the application on_resume can require some care, especially for GL heavy apps Other useful application events - on_stop, on_start, on_cong_changed

Slide 34

Slide 34 text

Dealing with hardware Current state of play pyjnius provides access to the native interfaces for Android hw = jnius.autoclass('org.renpy.android.Hardware') Still need to know all the java interface details Things work dierently on other platforms (pyobjus for iOS, etc)

Slide 35

Slide 35 text

Dealing with hardware Current state of play pyjnius provides access to the native interfaces for Android hw = jnius.autoclass('org.renpy.android.Hardware') Still need to know all the java interface details Things work dierently on other platforms (pyobjus for iOS, etc) The better future (still a work in progress)

Slide 36

Slide 36 text

Dealing with hardware Current state of play pyjnius provides access to the native interfaces for Android hw = jnius.autoclass('org.renpy.android.Hardware') Still need to know all the java interface details Things work dierently on other platforms (pyobjus for iOS, etc) The better future (still a work in progress) plyer Abstraction layer over common functionality Current aim is to support Camera, Accelerometer, Notications and Text to Speech Under reasonably active development - android support reasonably eshed out, but other platforms need work

Slide 37

Slide 37 text

Deployment to Android For ddling with kivy, kivy-launcher is great But not a genuine deployment option

Slide 38

Slide 38 text

Deployment to Android For ddling with kivy, kivy-launcher is great But not a genuine deployment option Need to compile android-for-python to generate an apk Lots of documentation and howtos Still all a bit tedious, though

Slide 39

Slide 39 text

Deployment to Android For ddling with kivy, kivy-launcher is great But not a genuine deployment option Need to compile android-for-python to generate an apk Lots of documentation and howtos Still all a bit tedious, though Recently started project - buildozer Unies a lot of the grunt work Everything handled by a single spec le Handles android and iOS targets

Slide 40

Slide 40 text

Deployment to Android For ddling with kivy, kivy-launcher is great But not a genuine deployment option Need to compile android-for-python to generate an apk Lots of documentation and howtos Still all a bit tedious, though Recently started project - buildozer Unies a lot of the grunt work Everything handled by a single spec le Handles android and iOS targets Will download missing components if needed when building the apk i.e. use with great care when behind a slow network connection