How can I create a 10x10 button grid in Kv Language?

192 views
Skip to first unread message

Rogério Dec

unread,
Feb 24, 2019, 8:51:16 PM2/24/19
to Kivy users support
As described here (https://stackoverflow.com/questions/54858229/how-can-i-create-a-10x10-button-grid-in-kivy-language), I'd like to know how can I create this 10x10 grid in Kv Language?

Elliot Garbus

unread,
Feb 24, 2019, 9:50:16 PM2/24/19
to kivy-...@googlegroups.com

GridLayout:
    rows: 10
    cols: 10
    Button:
        text: '1'
    Button:
        text: '2'

# ... fill in the buttons.

    Button:
        text: '100'

On 2/24/2019 6:51 PM, Rogério Dec wrote:
As described here (https://stackoverflow.com/questions/54858229/how-can-i-create-a-10x10-button-grid-in-kivy-language), I'd like to know how can I create this 10x10 grid in Kv Language?
--
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.
To post to this group, send email to kivy-...@googlegroups.com.
Visit this group at https://groups.google.com/group/kivy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5ec59ae5-5506-4304-a71b-4dc82e8dd79a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rogério Dec

unread,
Feb 24, 2019, 10:20:17 PM2/24/19
to Kivy users support
Well, that's a very prolix way! 
And if I had 1000 buttons, would I have to put 2000 rows? No way...
Is there no other better way in kv? 

Elliot Garbus

unread,
Feb 24, 2019, 10:55:22 PM2/24/19
to kivy-...@googlegroups.com

There are some things you could do to make it a little less tedious, but it will still be tedious in KV, if you have something so repetitive.

Others may have better suggestions…  
You could create a class of 10 buttons and instance it 10 times… if your interested I could type it up – but more importantly I think this is about the right tool for the job.

 

I use KV for more static elements that are in my GUI layout.  I typically will used nested BoxLayouts and GridLayouts to position and customize their appearance.  If I have a list of items, or something as repetitive as your 1000 buttons, I would code it in Python (as a class derived from a layout class, and use KV to position this repetitive section into the layout.

I would use KV to define a single button, select the fontsize, color, other attributes that might be unique the button.

 

I hope this is helpful.

Gabriel Pettier

unread,
Feb 25, 2019, 5:14:16 PM2/25/19
to kivy-...@googlegroups.com
You want to use RecycleView, with RecycleGriLayout.

Even if you don't want to scroll, but to display them all at the same
time, it's the simplest way to do such thing directly from kv.


from kivy.app import App
from kivy.lang import Builder

KV = '''
FloatLayout:
RecycleView:
data: [{'text': str(x)} for x in range(10 * 10)]
viewclass: 'Button'

RecycleGridLayout:
cols: 10
size_hint: 1, 1
default_size_hint: 1, 1
'''

class Board(App):
def build(self):
return Builder.load_string(KV)


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

here this example assumes you want to see all 100 widgets, if you want
to increase the number of widget, you probably want to remove the
`default_size_hint` part, because drawing thousands of widgets at the
same time is not a good idea performance-wise, with recycleview, only
the number of visible widgets will be created, and they'll be
transparently reused as you scroll.


from kivy.app import App
from kivy.lang import Builder

KV = '''
FloatLayout:
RecycleView:
data: [{'text': str(x)} for x in range(100 * 100)]
viewclass: 'Button'

RecycleGridLayout:
cols: 100
size_hint: None, None
size: self.minimum_size
default_size: 100, 100
default_size_hint: None, None
'''

class Board(App):
def build(self):
return Builder.load_string(KV)


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


this will scroll smoothly even if there are 10k theorical widgets,
because only a few dozens of them actually exists, and if you pushed it
to 1000x1000, it would still scroll smoothly, although creating the 1
000 000 of dicts will take some time at startup.
>To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/20190225035516.HTLC4162.fed1rmfepo203.cox.net%40fed1rmimpo209.cox.net.

Elliot Garbus

unread,
Feb 26, 2019, 10:18:07 AM2/26/19
to Kivy users support
That is brilliant!
I've avoided even really learning the recycle view, I have been frightened off by the warnings in the documentation:
'This module is highly experimental, its API may change in the future and the documentation is not complete at this time.'

Any comments on what is likely to change?  What is recommended to use?
Reply all
Reply to author
Forward
0 new messages