40

An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

An Introduction to Kivy

A tutorial of sorts

Neil Muller

4 October 2013

Page 2: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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)

Page 3: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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)

Page 4: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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)

Page 5: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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)

Page 6: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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)

Page 7: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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)

Page 8: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 9: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 10: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 11: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 12: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 13: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 14: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 15: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 16: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 17: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 18: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 19: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 20: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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.

Page 21: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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.

Page 22: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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.

Page 23: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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.

Page 24: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 25: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 26: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 27: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 28: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 29: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 30: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 31: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 32: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 33: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 34: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 35: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 36: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 37: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 38: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 39: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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

Page 40: An Introduction to Kivyneil/talks/pyconza/pyconza_2013/Kivy...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

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