我使用Python Kivy。 我想在带有5列的网格中显示包含标题和图像下面的卡片。

问题描述 投票:0回答:1
您可以使用

ScrollView

.

来设置

size_hint_y
的高度。
在这里是一个类似的
python kivy
1个回答
0
投票
,我认为您想要的是:

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()
	

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.