Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PyConZA 2013: "Introduction to Kivy" by Neil Muller

Pycon ZA
October 25, 2013

PyConZA 2013: "Introduction to Kivy" by Neil Muller

A overview of the kivy (www.kivy.org) framework for multi-touch apps.

I'll cover the basics of kivy as well as some of the advanced features, and discuss several aspects of using the framework for android app development.

People attending the tutorial are encouraged to install kivy before the tutorial. The instructions on the kivy site cover most cases. If installing in a virtualenv, note that kivy requires pygame and cython to install. For experimenting with kivy on android devices, I recommend that kivy launcher from the google play store.

Examples and additional materials are available on bitbucket at
https://bitbucket.org/drnlm/pyconza-2013-kivy-tutorial

Pycon ZA

October 25, 2013
Tweet

More Decks by Pycon ZA

Other Decks in Programming

Transcript

  1. An Introduction to Kivy A tutorial of sorts Neil Muller

    4 October 2013
  2. Introduction Why?

  3. Introduction Why? I want to write software that works on

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

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

    my phone In general, I'd rather be writing python What is kivy?
  6. 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
  7. 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)
  8. 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
  9. A Very Basic Kivy App Hello World

  10. 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
  11. 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
  12. KV Language Describes the layout of the application More complicated

    logic lives in the python le
  13. 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
  14. 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
  15. 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
  16. Properties Provide Validation & Value / Bounds checking

  17. Properties Provide Validation & Value / Bounds checking Can observe

    changes on the property
  18. 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
  19. 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
  20. Events Basic event is MotionEvent Although TouchEvent subclass is what

    we're almost always want
  21. 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
  22. 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
  23. 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.
  24. 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
  25. 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)
  26. 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
  27. Media Kivy provides easy image loading and manipulation functions Also

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

    support for sound & video Very useful cache implementation as well
  29. 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
  30. Conguration and other nifty stu Conguration system is based o

    CongParser Nifty mostly automatic cong screen setup
  31. 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
  32. 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
  33. 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
  34. 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)
  35. 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)
  36. 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
  37. Deployment to Android For ddling with kivy, kivy-launcher is great

    But not a genuine deployment option
  38. 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
  39. 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
  40. 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