I'm trying to scroll in a GridLayout, in which I created labels for different movie names.
I followed the Kivy documentaion and set one of the size_hints of the label to None. When I try to scroll nothing happends and the scroll bar doesn't appear either.
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
class Grid(GridLayout):
def __init__(self, **kwargs):
super(Grid, self).__init__(**kwargs)
movies = [
"It Happened One Night",
"Citizen Kane",
"All About Eve",
"The Irishman",
"Singin' in the Rain",
"Double Indemnity",
"L.A. Confidential"
]
self.rows = len(movies)
# Iterate over list and add labels
for i in movies:
self.add_widget(Label(text=i, size_hint_y=None, font_size=50))
class ScrollableView(ScrollView):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.do_scroll_x = False
self.do_scroll_y = True
self.size_hint_min = (0.5, 0.5)
self.add_widget(Grid())
class MyApp(App):
def build(self):
return ScrollableView()
if __name__ == "__main__":
app = MyApp()
app.run()
CodePudding user response:
The size_hint_y = None
is required for the child of the ScrollView
, but you are assigning it to the child of the child. Then you can bind the height
of the Grid
to the minimum_height
property. Also, using minimum_height
for the Grid
requires that the children of the Grid
have well defined heights
. Here is a modified version of your code that does that:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
class Grid(GridLayout):
def __init__(self, **kwargs):
super(Grid, self).__init__(**kwargs)
self.size_hint_y = None
movies = [
"It Happened One Night",
"Citizen Kane",
"All About Eve",
"The Irishman",
"Singin' in the Rain",
"Double Indemnity",
"L.A. Confidential"
]
self.bind(minimum_height=self.setter('height')) # this insures that the height of the Grid can contain all the children
self.rows = len(movies)
# Iterate over list and add labels
for i in movies:
self.add_widget(Label(text=i, size_hint_y=None, height=100, font_size=50)) # note height setting
class ScrollableView(ScrollView):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.do_scroll_x = False
self.do_scroll_y = True
self.size_hint_min = (0.5, 0.5)
self.add_widget(Grid())
class MyApp(App):
def build(self):
return ScrollableView()
if __name__ == "__main__":
app = MyApp()
app.run()