kivy scroll view

54 views
Skip to first unread message

Degenerate Tech

unread,
Aug 17, 2020, 3:59:24 AM8/17/20
to Kivy users support
how to scroll both x and y direction of widgets distributed in grid layout ?
img.png

Elliot Garbus

unread,
Aug 17, 2020, 12:18:47 PM8/17/20
to kivy-...@googlegroups.com

The thing to keep in mind with scrollview is that the items to be scrolled must be sized.

Here is a short example:

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

kv =
"""
BoxLayout:
    ScrollView:
        do_scroll_x: True
        do_scroll_y: True
        scroll_type:['bars', 'content']
        bar_width: 20
        ButtonGridLayout:
            rows:20
            cols:10
            id: sv_grid
            size_hint: None, None
            size: self.minimum_width, self.minimum_height
           
"""


class ButtonGridLayout(GridLayout):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
for i in range(200):
           
self.add_widget(Button(text=str(i), size_hint=(None,None), size=(200,48)))




class ScrollGridApp(App):
   
def build(self):
       
return Builder.load_string(kv)


ScrollGridApp().run()

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5da249fb-378c-4660-8cd9-7c9b4e0430c2o%40googlegroups.com.

 

img.png

Degenerate Tech

unread,
Aug 17, 2020, 10:45:50 PM8/17/20
to Kivy users support
wow...thank you sir...

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Degenerate Tech

unread,
Aug 17, 2020, 10:51:41 PM8/17/20
to Kivy users support
sir .tell me when you use both pure python and kv code as you did there ButtonGridLayout which will execute first ? kv code or  __init__()  of ButtonGridLayout?













On Monday, August 17, 2020 at 9:48:47 PM UTC+5:30, Elliot Garbus wrote:

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
Aug 18, 2020, 9:24:40 AM8/18/20
to kivy-...@googlegroups.com

The kv code is instancing the class, but the __init__ runs before the ids is setup or the attributes like rows and cols are set.  You can use the on_kv_post event to run code after the kv code for the widget has been processed.  In the example below, I moved the code from the __init__ to on_kv_post and used the number of cols and rows to create the appropriate number of buttons.

 

From the docs:

on_kv_post(base_widget, )

Fired after all the kv rules associated with the widget and all other widgets that are in any of those rules have had all their kv rules applied. base_widget is the base-most widget whose instantiation triggered the kv rules (i.e. the widget instantiated from Python, e.g. MyWidget()).

https://kivy.org/doc/stable/api-kivy.uix.widget.html?highlight=widget#kivy.uix.widget.Widget

 

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

kv =
"""

BoxLayout:
    ScrollView:
        do_scroll_x: True
        do_scroll_y: True
        scroll_type:['bars', 'content']
        bar_width: 10

        ButtonGridLayout:
            rows:20
            cols:10
            id: sv_grid
            size_hint: None, None
            size: self.minimum_width, self.minimum_height
           
"""


class ButtonGridLayout(GridLayout):
   
def on_kv_post(self, base_widget):
       
for i in range(self.rows * self.cols):
           
self.add_widget(Button(text=str(i), size_hint=(None,None), size=(200,48)))


To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/83adbffd-e600-4f9f-8331-74d1328bdc5eo%40googlegroups.com.

 

Degenerate Tech

unread,
Aug 18, 2020, 9:54:31 AM8/18/20
to Kivy users support
Reply all
Reply to author
Forward
0 new messages