Achieving Template result using Dynamic Classes in Kivy 1.8.0

380 views
Skip to first unread message

Roger Smith

unread,
Mar 20, 2014, 2:26:02 AM3/20/14
to kivy-...@googlegroups.com
Hi, I've been working on a small project to learn Kivy and have become stuck on something. I was hoping to replicate the solution shown here [ https://groups.google.com/forum/#!topic/kivy-users/rzkSXHzzz7Q ] but the Kivy Documentation mentions that templates were deprecated in 1.7.0 and that Dynamic Classes should be used instead. How would I go about achieving the same result using Dynamic Classes?

Roger Smith

unread,
Mar 25, 2014, 10:41:15 PM3/25/14
to kivy-...@googlegroups.com
Still having trouble with this, particularly how to substitute the 'ctx.dictkey' parts in the KV file. Please help!

Mathieu Virbel

unread,
Mar 26, 2014, 1:30:24 PM3/26/14
to kivy-...@googlegroups.com
Maybe that:

In python:

class DataView(BoxLayout):
    item_template = StringProperty("DataViewItem")
    items = ListProperty([])

    def on_items(self, *args):
        self.clear_widgets()
        for item in self.items:
            w = Factory.DataViewItem(**item)
            self.add_widget(w)

In kv:

<DataViewItem@BoxLayout>:
thumbnail: ''
title: ''

    Image:
        source: root.thumnail
    Button:
        text: root.title



--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Roger Smith

unread,
Mar 26, 2014, 10:03:04 PM3/26/14
to kivy-...@googlegroups.com
Thanks for your response Mathieu. It almost works, but returns a single button with no text, even when I insert {'title': 'value'}, {'title': 'value'} inside 'items'. I'm guessing I have to put something next to 'title:' and 'thumbnail:' in the kv file (as when I put a string next to 'title:' the string showed up as text on the button) but i'm not sure what.

Roger Smith

unread,
Apr 2, 2014, 1:11:41 AM4/2/14
to kivy-...@googlegroups.com
Just to wrap this up on a happy note, I decided to try the ListAdapter route, which I had originally thought would be too difficult. After looking through the Notes example (https://github.com/kivy/kivy/tree/master/examples/tutorials/notes), I managed to get what I wanted working without a great deal of complexity.

Have left the snippet in the event that it helps someone else out.

import kivy

kivy
.require('1.8.0')

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty, ListProperty, StringProperty
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.video import Video
from kivy.uix.scrollview import ScrollView
from kivy.uix.listview import ListItemButton, ListView
from kivy.factory import Factory
from kivy.adapters.listadapter import ListAdapter

Builder.load_string("""

#:kivy 1.8
#:import ListAdapter kivy.adapters.listadapter.ListAdapter
#:import Factory kivy.factory.Factory


<ExampleListItem>:
 height: '70sp'
    size_hint_y: None

    BoxLayout:
        Button:
            text: root.example_title
            on_press: app.root.examplevideo.source = root.example_url


<ExampleListView>:
 adapter: ListAdapter(data=app.data, cls=Factory.ExampleListItem, args_converter=root.args_converter)


<SomeOtherClass>:

"""
)

class ExampleListItem(BoxLayout):
 example_title
= StringProperty()
 example_url
= StringProperty()
 
class ExampleListView(ListView):
 
def args_converter(self, row_index, item):
 
return {'example_index': row_index, 'example_title': item['title'], 'example_url': item['url']}
 
class SomeOtherClass(FloatLayout):
 examplevideo
= ObjectProperty(None)

class ExampleApp(App):

 data
= ListProperty([
 
{'title': 'I am a Sample Button', 'url': 'https://archive.org/download/ThemeFromShafthip-hopRemixVideo/ThemeFromShaftb-boyRemix_512kb.mp4'},
 
{'title': 'No I am a Sample Button', 'url': 'https://archive.org/download/BADmichaeljackson/brandon_512kb.mp4'},
 
{'title': 'Bro R U SRS', 'url': 'https://archive.org/download/WeEditLife/WeEditLife_512kb.mp4'},
 
{'title': 'I R Button', 'url': 'https://archive.org/download/ThemeFromShafthip-hopRemixVideo/ThemeFromShaftb-boyRemix_512kb.mp4'},
 
{'title': 'Leave me out of this', 'url': 'https://archive.org/download/BADmichaeljackson/brandon_512kb.mp4'},
 
{'title': 'Me too', 'url': 'https://archive.org/download/WeEditLife/WeEditLife_512kb.mp4'},
 
{'title': 'BUTTON PARTY', 'url': 'https://archive.org/download/ThemeFromShafthip-hopRemixVideo/ThemeFromShaftb-boyRemix_512kb.mp4'},
 
{'title': 'The end of the button road', 'url': 'https://archive.org/download/BADmichaeljackson/brandon_512kb.mp4'}
 
])
 
 
def build(self):


if __name__ == '__main__':
   
ExampleApp().run()

Roger Smith

unread,
Apr 2, 2014, 1:15:17 AM4/2/14
to kivy-...@googlegroups.com
Whoops, make that:

import kivy

kivy.require('1.8.0')

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty, ListProperty, StringProperty
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.video import Video
from kivy.uix.scrollview import ScrollView
from kivy.uix.listview import ListItemButton, ListView
from kivy.factory import Factory
from kivy.adapters.listadapter import ListAdapter

class ExampleListItem(BoxLayout):
example_title = StringProperty()
example_url = StringProperty()
class ExampleListView(ListView):
def args_converter(self, row_index, item):
return {'example_index': row_index, 'example_title': item['title'], 'example_url': item['url']}
class SomeOtherClass(FloatLayout):
examplevideo = ObjectProperty(None)

Builder.load_string("""

#:kivy 1.8
#:import ListAdapter kivy.adapters.listadapter.ListAdapter
#:import Factory kivy.factory.Factory

<ExampleListItem>:
height: '70sp'
    size_hint_y: None

    BoxLayout:
        Button:
            text: root.example_title
            on_press: app.root.examplevideo.source = root.example_url

<ExampleListView>:
adapter: ListAdapter(data=app.data, cls=Factory.ExampleListItem, args_converter=root.args_converter)

<SomeOtherClass>:
examplevideo: examplevideo
ScrollView:
id: scroller
size_hint: (.5, .65)
pos_hint: {'center_x': .25, 'center_y': .575}
do_scroll_x: 'False'
ExampleListView
Video:
id: examplevideo
source: ''
state: 'play'
size_hint: (.4, .4)
pos_hint: {'center_x': .75, 'center_y': .625}
self.root = SomeOtherClass()
return self.root
Reply all
Reply to author
Forward
0 new messages