Skip to content

Instantly share code, notes, and snippets.

@pjz
Created March 10, 2012 02:06
Show Gist options
  • Save pjz/2009714 to your computer and use it in GitHub Desktop.
Save pjz/2009714 to your computer and use it in GitHub Desktop.
Kivy StringListView box v2
import kivy
kivy.require('1.0.9')
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, NumericProperty, ListProperty, ObjectProperty
Builder.load_string('''
[StringListViewItem@BoxLayout]:
orientation: 'horizontal'
Label:
text: ctx.text
on_touch_down: self.parent.parent.set_chosen(self)
''')
class StringListView(BoxLayout):
item_template = StringProperty('StringListViewItem')
items = ListProperty([])
chosen = ObjectProperty(None)
def on_items(self, *args):
self.orientation = 'vertical'
self.clear_widgets()
for item in self.items:
Clock.schedule_once(self._new_item_func(item), 0)
def _new_item_func(self, item):
itemd = item.copy()
def _(x):
w = Builder.template(self.item_template, **itemd)
self.add_widget(w)
return _
def _new_item(self, item):
w = Builder.template(self.item_template, **item)
print item
self.add_widget(w)
def set_chosen(self, widget):
self.chosen = widget
print "chose %s" % widget.text
if __name__ == '__main__':
from kivy.app import App
# App to test these widgets
class TestApp(App):
def build(self):
items = [ {'text':'item %d' % i} for i in range(20) ]
return StringListView(items=items)
TestApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment