-
Notifications
You must be signed in to change notification settings - Fork 55
Dev_Sprint1
Author::Mukul Chauhan
Original Article here
Purpose of this dev sprint was to get people familiarized with Kivy basics.
Date: August 5th 2017
Venue: Empowerers co working space
The meet-up started with a brief introduction about Kivy. As Python can be used to do almost anything then why not our android application ? Kivy is a python library used to make cross platform apps and that is what this meetup was all about, Building the basics that can be used to develop our own PyCon Android app using Kivy. So let’s get started, even if you missed this meetup, follow along you will be good to go. Starting with the Introduction
Kivy is an open source Python library for developing mobile apps and other multi-touch application software with a natural user interface (NUI). It can run on Android, iOS, Linux, OS X, and Windows. Distributed under the terms of the MIT license, Kivy is free and open source software.
Sounds good? One place to develop a app for all platforms. Yup, that’s what i thought. After this we installed kivy on our systems.
Installing Kivy is easy. But before that dependencies must be fulfilled.
For installation follow this Link
“Hello World!” App “Hello World!”, The program every programmer has written for sure. But we are not just writing a program here, we are writing a app.
Code::
from kivy.app import App
from kivy.uix.button import Button
class MyApp(App):
def build(self):
return Button(text = "Hello World")
if __name__ == '__main__':
MyApp().run()
Easy huh? It’s a big button with text “Hello World!” in it. But i want to do something with that button, lets change the button text according to its state. If the button is normal the text will be “Hello” and of button in pressed the text will be world.
Code ⇒
from kivy.app import App
from kivy.lang import Builder
class MyApp(App):
def build(self):
return Builder.load_string('''
Button
text: 'Hello' if self.state == 'normal' else 'World'
''')
if __name__ == '__main__':
MyApp().run()
Output ⇒
This this part we created an App with two pages. First page had a button “goto 2” and second page had a button “goto 1”, which worked accordingly. All the functionality was embedding in the kivy code.
Code ⇒
from kivy.app import App
from kivy.lang import Builder
class MyApp(App):
def build(self):
return Builder.load_string('''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManager
id: sm
transition: FadeTransition()
Screen
name: 'sc1'
Button
text: 'goto 2'
background_color: 1,0,1,1
on_release: sm.current = 'sc2'
Screen
name: 'sc2'
Button
text: 'goto 1'
on_release: sm.current = 'sc1'
''')
if __name__ == '__main__':
MyApp().run()
Output ⇒
Separating Kivy from Python
All that kivy code and python in a single file can be a big mess but good part is we can separate them. This was the last part where we learned how to separate kivy and python code and it is easy to do, just copy the kivy code in a new file named my.kv in the same directory.
If you don’t want to save the file with my.kv name Kivy won’t be able to recognize the .kv file it needs to reads from so you will need to load the file manually.
Code ⇒
from kivy.app import App
from kivy.lang import Builder
class MyApp(App):
def build(self):
return self.load_kv('4mykv.kv') #if not using my.kv as kv filename
if __name__ == '__main__':
MyApp().run()
My.kv file ⇒
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManager
id: sm
transition: FadeTransition()
Screen
name: 'sc1'
Button
text: 'goto 2'
background_color: 1,0,1,1
on_release: sm.current = 'sc2'
Screen
name: 'sc2'
Button
text: 'goto 1'
on_release: sm.current = 'sc1'
Output of this is same as of previous app, we just separated the kivy code from the python code. This way it looks clean and simple.