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

PyConZA 2013: "Introduction to Kivy" by Neil Mu...

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. Introduction Why? I want to write software that works on

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

    my phone In general, I'd rather be writing python What is kivy?
  3. 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
  4. 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)
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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.
  16. 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
  17. 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)
  18. 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
  19. Media Kivy provides easy image loading and manipulation functions Also

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

    CongParser Nifty mostly automatic cong screen setup
  22. 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
  23. 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
  24. 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
  25. 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)
  26. 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)
  27. 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
  28. 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
  29. 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
  30. 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