Most kivy apps start with this structure:
from kivy.app import App
class TutorialApp(App):
def build(self):
return
TutorialApp().run()
There is several way to go from here :
All the codes below (except example 1 and 3) have the same widget and similar features, but show different way to build the app.
Example 1: returning a single widget (simple Hello World App)
from kivy.app import App
from kivy.uix.button import Button
class TutorialApp(App):
def build(self):
return Button(text="Hello World!")
TutorialApp().run()
Example 2: returning several widgets + the button prints the label's text
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
class TutorialApp(App):
def build(self):
mylayout = BoxLayout(orientation="vertical")
mylabel = Label(text= "My App")
mybutton =Button(text="Click me!")
mylayout.add_widget(mylabel)
mybutton.bind(on_press= lambda a:print(mylabel.text))
mylayout.add_widget(mybutton)
return mylayout
TutorialApp().run()
Example 3: using a class (single widget) + the button prints "My Button"
from kivy.app import App
from kivy.uix.button import Button
class Mybutton(Button):
text="Click me!"
on_press =lambda a : print("My Button")
class TutorialApp(App):
def build(self):
return Mybutton()
TutorialApp().run()
Example 4: it's the same as ex. 2 but it shows how to use a class
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
class MyLayout(BoxLayout):
#You don't need to understand these 2 lines to make it work!
def __init__(self, **kwargs):
super(MyLayout, self).__init__(**kwargs)
self.orientation="vertical"
mylabel = Label(text= "My App")
self.add_widget(mylabel)
mybutton =Button(text="Click me!")
mybutton.bind(on_press= lambda a:print(mylabel.text))
self.add_widget(mybutton)
class TutorialApp(App):
def build(self):
return MyLayout()
TutorialApp().run()
Example 5: the same but showing how to use kv language within python
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
# BoxLayout: it's in the python part, so you need to import it
from kivy.lang import Builder
Builder.load_string("""
<MyLayout>
orientation:"vertical"
Label: # it's in the kv part, so no need to import it
id:mylabel
text:"My App"
Button:
text: "Click me!"
on_press: print(mylabel.text)
""")
class MyLayout(BoxLayout):
pass
class TutorialApp(App):
def build(self):
return MyLayout()
TutorialApp().run()
**Example 6: the same with the kv
part in a Tutorial.kv
file **
In .py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class MyLayout(BoxLayout):
pass
class TutorialApp(App):
#the kv file name will be Tutorial (name is before the "App")
def build(self):
return MyLayout()
TutorialApp().run()
In Tutorial.kv:
<MyLayout> # no need to import stuff in kv!
orientation:"vertical"
Label:
id:mylabel
text:"My App"
Button:
text: "Click me!"
on_press: print(mylabel.text)
**Example 7: link to specific kv file + a def in python receiving the label.text **
In .py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class MyLayout(BoxLayout):
def printMe(self_xx, yy):
print(yy)
class TutorialApp(App):
def build(self):
self.load_kv('myapp.kv')
return MyLayout()
TutorialApp().run()
In myapp.kv: orientation:"vertical" Label: id:mylabel text:"My App" Button: text: "Click me!" on_press: root.printMe(mylabel.text)
Example 8: the button prints the label's text (with a def in python using ids
(the "IDs") )
Notice that:
self_xx
from example 7 is replaced by self
In .py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class MyLayout(BoxLayout):
def printMe(self):
print(self.ids.mylabel.text)
class TutorialApp(App):
def build(self):
self.load_kv('myapp.kv')
return MyLayout()
TutorialApp().run()
In myapp.kv:
<MyLayout>
orientation:"vertical"
Label:
id:mylabel
text:"My App"
Button:
text: "Click me!"
on_press: root.printMe()
Example 9: the button prints the label's text (with a def in python using StringProperty)
In .py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
class MyLayout(BoxLayout):
stringProperty_mylabel= StringProperty("My App")
def printMe(self):
print(self.stringProperty_mylabel)
class TutorialApp(App):
def build(self):
return MyLayout()
TutorialApp().run()
In Tutorial.kv:
<MyLayout>
orientation:"vertical"
Label:
id:mylabel
text:root.stringProperty_mylabel
Button:
text: "Click me!"
on_press: root.printMe()
Example 10: the button prints the label's text (with a def in python using ObjectProperty)
In .py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class MyLayout(BoxLayout):
objectProperty_mylabel= ObjectProperty(None)
def printMe(self):
print(self.objectProperty_mylabel.text)
class TutorialApp(App):
def build(self):
return MyLayout()
TutorialApp().run()
In Tutorial.kv:
<MyLayout>
orientation:"vertical"
objectProperty_mylabel:mylabel
Label:
id:mylabel
text:"My App"
Button:
text: "Click me!"
on_press: root.printMe()