Kivy Tutorial
Kivy Tutorial
import kivy
kivy.require("1.9.0")
class SampBoxLayout(BoxLayout):
# For checkbox
checkbox_is_active = ObjectProperty(False)
def checkbox_18_clicked(self, instance, value):
if value is True:
print("Checkbox Checked")
else:
print("Checkbox Unchecked")
# For Switch
def switch_on(self, instance, value):
if value is True:
print("Switch On")
else:
print("Switch Off")
# For Spinner
def spinner_clicked(self, value):
print("Spinner Value " + value)
class SampleApp(App):
def build(self):
sample_app = SampleApp()
sample_app.run()
# ---------- sample.kv ----------
<CustLabel@Label>:
color: 0, 0, 0, 1
<CustomPopup>:
size_hint: .5, .5
auto_dismiss: False
title: "The Popup"
Button:
text: "Close"
on_press: root.dismiss()
SampBoxLayout:
<SampBoxLayout>:
orientation: "vertical"
padding: 10
spacing: 10
BoxLayout:
orientation: "horizontal"
size_hint_x: .22
CustLabel:
text: "Are you over 18"
size_hint_x: .80
CheckBox:
on_active: root.checkbox_18_clicked(self, self.active)
size_hint_x: .20
BoxLayout:
orientation: "horizontal"
size_hint_x: .55
CustLabel:
text: "Favorite Color"
color: 0, 0, 0, 1
size_hint_x: .265
CheckBox:
group: "fav_color"
value: root.blue
size_hint_x: .05
CustLabel:
text: "Blue"
color: 0, 0, 0, 1
size_hint_x: .15
CheckBox:
group: "fav_color"
value: root.red
size_hint_x: .05
CustLabel:
text: "Red"
color: 0, 0, 0, 1
size_hint_x: .15
CheckBox:
group: "fav_color"
value: root.green
size_hint_x: .05
CustLabel:
text: "Green"
color: 0, 0, 0, 1
size_hint_x: .15
BoxLayout:
orientation: "horizontal"
size_hint_x: .25
CustLabel:
text: str(slider_id.value)
BoxLayout:
orientation: "horizontal"
size_hint_x: .25
CustLabel:
text: "On / Off"
Switch:
id: switch_id
on_active: root.switch_on(self, self.active)
BoxLayout:
orientation: "horizontal"
size_hint_x: .25
BoxLayout:
orientation: "horizontal"
size_hint_x: .25
Spinner:
text: "First"
values: ["First", "Second", "Third"]
id: spinner_id
on_text: root.spinner_clicked(spinner_id.text)
BoxLayout:
orientation: "horizontal"
size_hint_x: .25
TabbedPanel:
do_default_tab: False
TabbedPanelItem:
text: "1st Tab"
Label:
text: "Content of First Panel"
TabbedPanelItem:
text: "2nd Tab"
Label:
text: "Content of Second Panel"
TabbedPanelItem:
text: "3rd Tab"
Label:
text: "Content of Third Panel"
import kivy
kivy.require('1.9.0')
<ScreenTwo>:
BoxLayout:
Button:
text: "Go to Screen 1"
on_press:
root.manager.transition.direction = 'right'
root.manager.current = 'screen_one'
""")
# Create a class for all screens in which you can include
# helpful methods specific to that screen
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class KivyTut2App(App):
def build(self):
return screen_manager
sample_app = KivyTut2App()
sample_app.run()