SlideShare a Scribd company logo
KIVY
NUI Applications with Python
Python Meetup Innsbruck | 25. April 2017
Robert Niederreiter
CROSS PLATFORM
kivy
ARCHITECTURE
Widget Kv Language
Cache Clock Gesture Event Loop Properties
Core Providers
Window
Text
Image
Video
Audio
Graphics
Vertex Buffer
Frame Buffer
Texture
Shader
Instructions
Inputs
Motion Event
Post Processing
(double tab,
Dejitter, …)
Pygame PIL Gstreamer
FFMpeg SDL Cairo
GLES API GLEW
MouseTUIO WM_Touch
Mac TouchMTDev HIDInput
App brought to foreground
After process is killed (Android/iOS)
Kivy bootstrap for Android/iOS
Python start, run()
build()
on_start()
Apps functionson_resume()
Extenal App/OS or internal
function pauses App
on_pause()
Save your work here.
Resume is not guaranteed.
on_stop()
Kivy window destroyed
Python stop
on_pause()
Resume?
return True
return False
NoYes
LIFECYCLE
EVENT LOOP
Clock Events Loop
Filesystem
Network
Process
Other
Motion Events
Post Processing
(double tab, swipe, ...)
Input Processing
Event
Dispatcher
Dispatch Input Events
Custom Events
Property Events
Window Events
GUI
Touch
Mouse
Keyboard
Joystick
Other
Input Kivy Main Thread
Non GUI
Operations
(may run in
dedicated thread)
Main
Loop
EventDispatcher
App
Widget
Widgets are self-contained
and organized as tree
Animation
Clock
MotionEvent
Layout
Controls size and
position of it's children
FloatLayout
BoxLayout
GridLayout
ButtonBehavior
CoverBehavior
DragBehavior
behaviors
...
Button
Switch
Slider
Popup
...
uix
graphics
Widget representation is done using canvas, graphics
instructions are applied to it
ContextInstruction VertexInstruction
Color
Rotate
Scale
...
Triangle
Rectangle
Ellipse
...
Kv Language
The KV language (sometimes called kvlang, or kivy language),
allows you to create your widget tree in a declarative way
and to bind widget properties to each other or to callbacks in a
natural manner.
...
OBJECTS
Mixin Classes for Widgets
APPLICATION
import kivy
# replace with your current kivy version
kivy.require('1.9.1')
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
# MyApp inherits from App object
def build(self):
# build returns the root widget
return Label(text='Hello world')
if __name__ == '__main__':
MyApp().run()
REPETITIVE EVENTS
from kivy.clock import Clock
def my_callback(dt):
# ``dt`` is delta time
print('My callback is called'.format(dt))
# call ``my_callback`` X times per second
event = Clock.schedule_interval(my_callback, 1 / 30.)
# unscedule can be done by either using
event.cancel()
# or
Clock.unschedule(event)
def my_callback(dt):
# if repetitive callback returns False, it gets unscheduled as well
if some_condition:
return False
# regular processing
Clock.schedule_interval(my_callback, 1 / 30.)
ONE-TIME EVENTS
from kivy.clock import Clock
def my_callback(dt):
print('My callback is called'.format(dt))
# call ``my_callback`` once in one second
Clock.schedule_once(my_callback, 1)
The second argument is the amount of time to wait before calling the function,
in seconds. However, you can achieve some other results with special values
for the second argument:
● If X is greater than 0, the callback will be called in X seconds
● If X is 0, the callback will be called after the next frame
● If X is -1, the callback will be called before the next frame
INPUT AND PROPERTY EVENTS
from kivy.uix.widget import Widget
from kivy.properties import ListProperty
class CustomBtn(Widget):
# it's possible to bind to kivy property changes
pressed = ListProperty([0, 0])
def on_touch_down(self, touch):
# event handler for regular touch events
if self.collide_point(*touch.pos):
# setting pressed property triggers property event
self.pressed = touch.pos
# return True to indicate event consumed
return True
# delegate touch event to super class
return super(CustomBtn, self).on_touch_down(touch)
def on_pressed(self, instance, pos):
# event handler triggered if ``pressed`` property changed
print('pressed at {pos}'.format(pos=pos))
CUSTOM EVENTS
from kivy.event import EventDispatcher
class MyEventDispatcher(EventDispatcher):
def __init__(self, **kw):
# register custom event
self.register_event_type('on_test')
super(MyEventDispatcher, self).__init__(**kw)
def do_something(self, value):
# when do_something is called, the 'on_test' event will be
# dispatched with the value
self.dispatch('on_test', value)
def on_test(self, *args):
# default event handler
print('I am dispatched'.format(args))
def my_callback(value, *args):
print('Hello, I got an event!'.format(args))
ev = MyEventDispatcher() # instanciate ``MyEventDispatcher``
ev.bind(on_test=my_callback) # bind ``my_callback`` to ``on_test``
ev.do_something('test') # ``do_something`` dispatches ``on_test``
WIDGET TREE
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
# create widget and add children
layout = BoxLayout(padding=10)
button = Button(text='My first button')
layout.add_widget(button)
# remove child from widget
layout.remove_widget(button)
# clear children
layout.clear_widgets()
# traversing the tree, iterate widget children
for child in layout.children:
print(child)
# traversing the tree, access parent widget
child = layout.children[0]
Layout = child.parent
# setting a z-index for a child widget
layout.add_widget(widget, index)
KV LANGUAGE
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
Builder.load_string('''
<CustomLayout>
Button:
text: 'Hello World!!'
size_hint: .5, .5
pos_hint: {'center_x': .5, 'center_y': .5}
''')
class CustomLayout(FloatLayout):
pass
As your application grow more complex, it’s common that the construction of widget
trees and explicit declaration of bindings, becomes verbose and hard to maintain.
The KV Language is a attempt to overcome these short-comings.
In the example above KV code is defined inside python module as docstring passed
to Builder.load_string. KV code can also be contained in a separate
file which can be loaded by using Builder.load_file('path/to/file.kv')
GRAPHICS
class MyWidget(Widget):
def __init__(self, **kw):
super(MyWidget, self).__init__(**kw)
with self.canvas:
# add your instruction for main canvas here
Color(1, 0, .4, mode='rgb')
Line(points=(x1, y1, x2, y2, x3, y3))
with self.canvas.before:
# you can use this to add instructions rendered before
with self.canvas.after:
# you can use this to add instructions rendered after
MyWidget:
canvas:
Color:
rgba: 1, .3, .8, .5
Line:
points: zip(self.data.x, self.data.y)
Graphics instructions in Python
Graphics instructions in KV language
Thank You!
References:
https://kivy.org/docs/guide/basic.html
Credits:
Photos by:
https://www.flickr.com/photos/photos_by_chrystal
https://www.flickr.com/photos/steffireichert
https://creativecommons.org/licenses/by-nc-nd/2.0

More Related Content

What's hot

Introduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra SolutionsIntroduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra SolutionsQUONTRASOLUTIONS
 
Embedded_Linux_Booting
Embedded_Linux_BootingEmbedded_Linux_Booting
Embedded_Linux_BootingRashila Rr
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/CoreShay Cohen
 
Yocto Project introduction
Yocto Project introductionYocto Project introduction
Yocto Project introductionYi-Hsiu Hsu
 
XPDDS19 Keynote: Xen Dom0-less - Stefano Stabellini, Principal Engineer, Xilinx
XPDDS19 Keynote: Xen Dom0-less - Stefano Stabellini, Principal Engineer, XilinxXPDDS19 Keynote: Xen Dom0-less - Stefano Stabellini, Principal Engineer, Xilinx
XPDDS19 Keynote: Xen Dom0-less - Stefano Stabellini, Principal Engineer, XilinxThe Linux Foundation
 
Browsing Linux Kernel Source
Browsing Linux Kernel SourceBrowsing Linux Kernel Source
Browsing Linux Kernel SourceMotaz Saad
 
UM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of SoftwareUM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of SoftwareBrendan Gregg
 
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime RipardKernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime RipardAnne Nicolas
 
Xen on ARM for embedded and IoT: from secure containers to dom0less systems
Xen on ARM for embedded and IoT: from secure containers to dom0less systemsXen on ARM for embedded and IoT: from secure containers to dom0less systems
Xen on ARM for embedded and IoT: from secure containers to dom0less systemsStefano Stabellini
 
Linux Interrupts
Linux InterruptsLinux Interrupts
Linux InterruptsKernel TLV
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequenceHoucheng Lin
 
Meet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracingMeet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracingViller Hsiao
 

What's hot (20)

Introduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra SolutionsIntroduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra Solutions
 
Embedded_Linux_Booting
Embedded_Linux_BootingEmbedded_Linux_Booting
Embedded_Linux_Booting
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/Core
 
Yocto Project introduction
Yocto Project introductionYocto Project introduction
Yocto Project introduction
 
XPDDS19 Keynote: Xen Dom0-less - Stefano Stabellini, Principal Engineer, Xilinx
XPDDS19 Keynote: Xen Dom0-less - Stefano Stabellini, Principal Engineer, XilinxXPDDS19 Keynote: Xen Dom0-less - Stefano Stabellini, Principal Engineer, Xilinx
XPDDS19 Keynote: Xen Dom0-less - Stefano Stabellini, Principal Engineer, Xilinx
 
U-Boot - An universal bootloader
U-Boot - An universal bootloader U-Boot - An universal bootloader
U-Boot - An universal bootloader
 
Browsing Linux Kernel Source
Browsing Linux Kernel SourceBrowsing Linux Kernel Source
Browsing Linux Kernel Source
 
UM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of SoftwareUM2019 Extended BPF: A New Type of Software
UM2019 Extended BPF: A New Type of Software
 
Qemu Introduction
Qemu IntroductionQemu Introduction
Qemu Introduction
 
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime RipardKernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
 
Linux Network Stack
Linux Network StackLinux Network Stack
Linux Network Stack
 
Qemu Pcie
Qemu PcieQemu Pcie
Qemu Pcie
 
A practical guide to buildroot
A practical guide to buildrootA practical guide to buildroot
A practical guide to buildroot
 
Linux Internals - Part III
Linux Internals - Part IIILinux Internals - Part III
Linux Internals - Part III
 
Xen on ARM for embedded and IoT: from secure containers to dom0less systems
Xen on ARM for embedded and IoT: from secure containers to dom0less systemsXen on ARM for embedded and IoT: from secure containers to dom0less systems
Xen on ARM for embedded and IoT: from secure containers to dom0less systems
 
Linux Interrupts
Linux InterruptsLinux Interrupts
Linux Interrupts
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
 
Meet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracingMeet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracing
 
Qemu
QemuQemu
Qemu
 
Pdp11 on-fpga
Pdp11 on-fpgaPdp11 on-fpga
Pdp11 on-fpga
 

Similar to Kivy Talk Python Meetup Innsbruck 2017.04.25

The Ring programming language version 1.5.1 book - Part 175 of 180
The Ring programming language version 1.5.1 book - Part 175 of 180 The Ring programming language version 1.5.1 book - Part 175 of 180
The Ring programming language version 1.5.1 book - Part 175 of 180 Mahmoud Samir Fayed
 
2011 py con
2011 py con2011 py con
2011 py conEing Ong
 
Programming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for AndroidProgramming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for AndroidEmanuele Di Saverio
 
The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210Mahmoud Samir Fayed
 
Building a js widget
Building a js widgetBuilding a js widget
Building a js widgetTudor Barbu
 
Titanium Appcelerator - Beginners
Titanium Appcelerator - BeginnersTitanium Appcelerator - Beginners
Titanium Appcelerator - BeginnersAmbarish Hazarnis
 
CQRS and Event Sourcing in a Symfony application
CQRS and Event Sourcing in a Symfony applicationCQRS and Event Sourcing in a Symfony application
CQRS and Event Sourcing in a Symfony applicationSamuel ROZE
 
How to dispatch redux action with timeout
How to dispatch redux action with timeoutHow to dispatch redux action with timeout
How to dispatch redux action with timeoutBOSC Tech Labs
 
«Gevent — быть или не быть?» Александр Мокров, Positive Technologies
«Gevent — быть или не быть?» Александр Мокров, Positive Technologies«Gevent — быть или не быть?» Александр Мокров, Positive Technologies
«Gevent — быть или не быть?» Александр Мокров, Positive Technologiesit-people
 
Asynchronous JS in Odoo
Asynchronous JS in OdooAsynchronous JS in Odoo
Asynchronous JS in OdooOdoo
 
Automation with Ansible and Containers
Automation with Ansible and ContainersAutomation with Ansible and Containers
Automation with Ansible and ContainersRodolfo Carvalho
 
Writing videogames with titanium appcelerator
Writing videogames with titanium appceleratorWriting videogames with titanium appcelerator
Writing videogames with titanium appceleratorAlessio Ricco
 
MOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app developmentMOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app developmentanistar sung
 

Similar to Kivy Talk Python Meetup Innsbruck 2017.04.25 (20)

GWT MVP Case Study
GWT MVP Case StudyGWT MVP Case Study
GWT MVP Case Study
 
The Ring programming language version 1.5.1 book - Part 175 of 180
The Ring programming language version 1.5.1 book - Part 175 of 180 The Ring programming language version 1.5.1 book - Part 175 of 180
The Ring programming language version 1.5.1 book - Part 175 of 180
 
2011 py con
2011 py con2011 py con
2011 py con
 
Programming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for AndroidProgramming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for Android
 
The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210
 
09events
09events09events
09events
 
Day 5
Day 5Day 5
Day 5
 
Building a js widget
Building a js widgetBuilding a js widget
Building a js widget
 
Titanium Appcelerator - Beginners
Titanium Appcelerator - BeginnersTitanium Appcelerator - Beginners
Titanium Appcelerator - Beginners
 
CQRS and Event Sourcing in a Symfony application
CQRS and Event Sourcing in a Symfony applicationCQRS and Event Sourcing in a Symfony application
CQRS and Event Sourcing in a Symfony application
 
How to dispatch redux action with timeout
How to dispatch redux action with timeoutHow to dispatch redux action with timeout
How to dispatch redux action with timeout
 
Gevent be or not to be
Gevent be or not to beGevent be or not to be
Gevent be or not to be
 
«Gevent — быть или не быть?» Александр Мокров, Positive Technologies
«Gevent — быть или не быть?» Александр Мокров, Positive Technologies«Gevent — быть или не быть?» Александр Мокров, Positive Technologies
«Gevent — быть или не быть?» Александр Мокров, Positive Technologies
 
Asynchronous JS in Odoo
Asynchronous JS in OdooAsynchronous JS in Odoo
Asynchronous JS in Odoo
 
Automation with Ansible and Containers
Automation with Ansible and ContainersAutomation with Ansible and Containers
Automation with Ansible and Containers
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Writing videogames with titanium appcelerator
Writing videogames with titanium appceleratorWriting videogames with titanium appcelerator
Writing videogames with titanium appcelerator
 
NestJS
NestJSNestJS
NestJS
 
MOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app developmentMOPCON 2014 - Best software architecture in app development
MOPCON 2014 - Best software architecture in app development
 
mobl
moblmobl
mobl
 

Recently uploaded

WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationWave PLM
 
CompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdfCompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdfFurqanuddin10
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesNeo4j
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion Clinic
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAlluxio, Inc.
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Gáspár Nagy
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandIES VE
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownloadvrstrong314
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Soroosh Khodami
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...Alluxio, Inc.
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAlluxio, Inc.
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?XfilesPro
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfmbmh111980
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowPeter Caitens
 
Benefits of Employee Monitoring Software
Benefits of  Employee Monitoring SoftwareBenefits of  Employee Monitoring Software
Benefits of Employee Monitoring SoftwareMera Monitor
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1KnowledgeSeed
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTier1 app
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with StrimziStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzisteffenkarlsson2
 

Recently uploaded (20)

WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
CompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdfCompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdf
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Benefits of Employee Monitoring Software
Benefits of  Employee Monitoring SoftwareBenefits of  Employee Monitoring Software
Benefits of Employee Monitoring Software
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with StrimziStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
 

Kivy Talk Python Meetup Innsbruck 2017.04.25

  • 1. KIVY NUI Applications with Python Python Meetup Innsbruck | 25. April 2017 Robert Niederreiter
  • 3. ARCHITECTURE Widget Kv Language Cache Clock Gesture Event Loop Properties Core Providers Window Text Image Video Audio Graphics Vertex Buffer Frame Buffer Texture Shader Instructions Inputs Motion Event Post Processing (double tab, Dejitter, …) Pygame PIL Gstreamer FFMpeg SDL Cairo GLES API GLEW MouseTUIO WM_Touch Mac TouchMTDev HIDInput
  • 4. App brought to foreground After process is killed (Android/iOS) Kivy bootstrap for Android/iOS Python start, run() build() on_start() Apps functionson_resume() Extenal App/OS or internal function pauses App on_pause() Save your work here. Resume is not guaranteed. on_stop() Kivy window destroyed Python stop on_pause() Resume? return True return False NoYes LIFECYCLE
  • 5. EVENT LOOP Clock Events Loop Filesystem Network Process Other Motion Events Post Processing (double tab, swipe, ...) Input Processing Event Dispatcher Dispatch Input Events Custom Events Property Events Window Events GUI Touch Mouse Keyboard Joystick Other Input Kivy Main Thread Non GUI Operations (may run in dedicated thread) Main Loop
  • 6. EventDispatcher App Widget Widgets are self-contained and organized as tree Animation Clock MotionEvent Layout Controls size and position of it's children FloatLayout BoxLayout GridLayout ButtonBehavior CoverBehavior DragBehavior behaviors ... Button Switch Slider Popup ... uix graphics Widget representation is done using canvas, graphics instructions are applied to it ContextInstruction VertexInstruction Color Rotate Scale ... Triangle Rectangle Ellipse ... Kv Language The KV language (sometimes called kvlang, or kivy language), allows you to create your widget tree in a declarative way and to bind widget properties to each other or to callbacks in a natural manner. ... OBJECTS Mixin Classes for Widgets
  • 7. APPLICATION import kivy # replace with your current kivy version kivy.require('1.9.1') from kivy.app import App from kivy.uix.label import Label class MyApp(App): # MyApp inherits from App object def build(self): # build returns the root widget return Label(text='Hello world') if __name__ == '__main__': MyApp().run()
  • 8. REPETITIVE EVENTS from kivy.clock import Clock def my_callback(dt): # ``dt`` is delta time print('My callback is called'.format(dt)) # call ``my_callback`` X times per second event = Clock.schedule_interval(my_callback, 1 / 30.) # unscedule can be done by either using event.cancel() # or Clock.unschedule(event) def my_callback(dt): # if repetitive callback returns False, it gets unscheduled as well if some_condition: return False # regular processing Clock.schedule_interval(my_callback, 1 / 30.)
  • 9. ONE-TIME EVENTS from kivy.clock import Clock def my_callback(dt): print('My callback is called'.format(dt)) # call ``my_callback`` once in one second Clock.schedule_once(my_callback, 1) The second argument is the amount of time to wait before calling the function, in seconds. However, you can achieve some other results with special values for the second argument: ● If X is greater than 0, the callback will be called in X seconds ● If X is 0, the callback will be called after the next frame ● If X is -1, the callback will be called before the next frame
  • 10. INPUT AND PROPERTY EVENTS from kivy.uix.widget import Widget from kivy.properties import ListProperty class CustomBtn(Widget): # it's possible to bind to kivy property changes pressed = ListProperty([0, 0]) def on_touch_down(self, touch): # event handler for regular touch events if self.collide_point(*touch.pos): # setting pressed property triggers property event self.pressed = touch.pos # return True to indicate event consumed return True # delegate touch event to super class return super(CustomBtn, self).on_touch_down(touch) def on_pressed(self, instance, pos): # event handler triggered if ``pressed`` property changed print('pressed at {pos}'.format(pos=pos))
  • 11. CUSTOM EVENTS from kivy.event import EventDispatcher class MyEventDispatcher(EventDispatcher): def __init__(self, **kw): # register custom event self.register_event_type('on_test') super(MyEventDispatcher, self).__init__(**kw) def do_something(self, value): # when do_something is called, the 'on_test' event will be # dispatched with the value self.dispatch('on_test', value) def on_test(self, *args): # default event handler print('I am dispatched'.format(args)) def my_callback(value, *args): print('Hello, I got an event!'.format(args)) ev = MyEventDispatcher() # instanciate ``MyEventDispatcher`` ev.bind(on_test=my_callback) # bind ``my_callback`` to ``on_test`` ev.do_something('test') # ``do_something`` dispatches ``on_test``
  • 12. WIDGET TREE from kivy.uix.boxlayout import BoxLayout from kivy.uix.button import Button # create widget and add children layout = BoxLayout(padding=10) button = Button(text='My first button') layout.add_widget(button) # remove child from widget layout.remove_widget(button) # clear children layout.clear_widgets() # traversing the tree, iterate widget children for child in layout.children: print(child) # traversing the tree, access parent widget child = layout.children[0] Layout = child.parent # setting a z-index for a child widget layout.add_widget(widget, index)
  • 13. KV LANGUAGE from kivy.uix.floatlayout import FloatLayout from kivy.lang import Builder Builder.load_string(''' <CustomLayout> Button: text: 'Hello World!!' size_hint: .5, .5 pos_hint: {'center_x': .5, 'center_y': .5} ''') class CustomLayout(FloatLayout): pass As your application grow more complex, it’s common that the construction of widget trees and explicit declaration of bindings, becomes verbose and hard to maintain. The KV Language is a attempt to overcome these short-comings. In the example above KV code is defined inside python module as docstring passed to Builder.load_string. KV code can also be contained in a separate file which can be loaded by using Builder.load_file('path/to/file.kv')
  • 14. GRAPHICS class MyWidget(Widget): def __init__(self, **kw): super(MyWidget, self).__init__(**kw) with self.canvas: # add your instruction for main canvas here Color(1, 0, .4, mode='rgb') Line(points=(x1, y1, x2, y2, x3, y3)) with self.canvas.before: # you can use this to add instructions rendered before with self.canvas.after: # you can use this to add instructions rendered after MyWidget: canvas: Color: rgba: 1, .3, .8, .5 Line: points: zip(self.data.x, self.data.y) Graphics instructions in Python Graphics instructions in KV language