Question about loops in .kv file

54 views
Skip to first unread message

aminet...@gmail.com

unread,
Aug 20, 2016, 9:35:31 PM8/20/16
to Kivy users support
Hey
thanks for Kivy, you're awesome
I am new to Kivy so this question might look like a little bit odd

I have list of objects in my python code and i want to create a button for each one in my .kv file
is there a way to loop throught them in .kv file?
something like Mako template language:
%for item in items

Thanks :)

ZenCODE

unread,
Aug 20, 2016, 10:13:34 PM8/20/16
to Kivy users support
You don't really want to do that. Kivy is really for layout, not logic. Loops should (probably) stay in Python as they are logical structures. It sounds like the best approach might be to subclass a Button, add the properties and intelligence you need. You can then just pop these into the appropriate layout defined in your kv....

If this does not work for you, perhaps try and demonstrate what you are trying to do?

aminet...@gmail.com

unread,
Aug 21, 2016, 5:23:47 AM8/21/16
to Kivy users support
this is what I was trying to accomplish in kivy, but I defined it in logic.
resp = get_lessons().json()
for l in resp['lessons']:
self.add_widget(
Button(text=l['title'])
)

thanks for your reply :)

ZenCODE

unread,
Aug 21, 2016, 1:32:50 PM8/21/16
to Kivy users support

Without knowing where you are doing this, that looks fine. You could put that in a list comprehension in kv, something like:


on_enter: [self.add_widget(Button(text=l['title'])) for get_lessons().json()['lessons']]


But really, it's simpler in Python. kv is great for layouts, but you really you don't need to do everything in kv...:-)



aminet...@gmail.com

unread,
Aug 21, 2016, 11:41:18 PM8/21/16
to Kivy users support
Thanks again :)
yes you are right. logic in python and layout in kivy is the best practice
Reply all
Reply to author
Forward
0 new messages