Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
usage demonstration of ids, dynamic classes/Factory, and doing a default binding using them, plus ScreenManager
Little demonstration of various kivy useful features:
- loading kv code
- adding dynamic parts to static parts using ids
- dynamic classes
- getting a reference to a Dynamic classes using Factory
- binding events to python expressions on kv side
useful references:
from import App
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen
from string import letters
# this kv code defines two things:
# - the root widget, a vertical boxlayout with:
# - the first element being another boxlayout to store buttons, with a
# "button" id
# - the second element being the ScreenManager that will contain our
# screens, with "sm" id
# - a dynamic class rule, that defines the `MahButton` class, based on
# button, and whose only difference with button is that on_press, it
# changes the screen manager's screen to the one with the name equals
# to the button's text.
# This works by using using the app's root widget, and its `ids`
# member, that maps the ids defined in the root widget's rule (ids
# being local to rules), to get the ScreenManager object and change
# its current screen. self is the current widget, here any `MahButton`
# instance that is currently being pressed, so self.text is the text
# displayed by the clicked button.
kv = '''
orientation: 'vertical'
size_hint_y: None
height: 30
id: buttons
id: sm
on_press: = self.text
class MahButton(Button):
class MahApp(App):
Our main application
def build(self):
# here we load the kv code, so we get the root rule as root, and
# the MahButton dynamic class is defined
self.root = Builder.load_string(kv)
for l in letters:
# create a screen with a Label as content, and add it to the
# ScreenManager object, again using ids, since the rule has
# been loaded.
s = Screen(name=l)
# add a button with the same text as the screen name to the
# `buttons` id of root, using the same ids technique
# we need to return the root widget
return self.root
# common python idiom to make programs that behave well if imported as
# modules
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment