ScrollView
来设置
size_hint_y
的高度。在这里是一个类似的
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty, ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
kv = '''
ScrollView:
size_hint_y: 0.75 # custom percentage of the window height
do_scroll_x: False
do_scroll_y: True
smooth_scroll_end: 15
scroll_wheel_distance: 20
GridLayout:
id: grid
cols: 5
spacing: 1, 5
size_hint: 1, None
height: self.minimum_height # must be allowed to grow for ScrollView to work
<Card>:
orientation: 'vertical'
size_hint: 1, None
height: 150 # must have a specified height for minimum_height to work in GridLayout
Button:
text: root.text
on_press: print(f"Button pressed: {self.text}")
size_hint_y: 0.25
Image:
texture: root.texture
size_hint_y: 0.75
'''
class Card(BoxLayout):
text = StringProperty('')
texture = ObjectProperty(None)
class MyApp(App):
def build(self):
Clock.schedule_once(self.fill_grid)
return Builder.load_string(kv)
def fill_grid(self, _dt):
grid = self.root.ids.grid
texture = Image(source='tester.png').texture
for index in range(50):
grid.add_widget(Card(text='record #' + str(index), texture=texture))
if __name__ == '__main__':
MyApp().run()