如何在Kivy Python中正确使用ScrollView?

问题描述 投票:0回答:1

我有此代码想要更改按钮的位置,但是如果我更改位置,则滚动将不再起作用。如何设法使其运作?以下是工作版本。如果我将size_hint:1,.1更改为size_hint:1,.7,则滚动不再起作用...

from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.floatlayout import FloatLayout

Builder.load_string('''
<Root>:
    ScrollView:
        size_hint: 1, .1
        GridLayout:
            size_hint_y: None
            cols: 1
            # minimum_height: self.height
            Button
                text: 'one'
            Button:
                text: 'two'
            Button:
                text: 'three'
            Button:
                text: 'four'
''')

class Root(FloatLayout):
    pass

class DemoApp(App):

    def build(self):
        return Root()

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


python scroll grid kivy grid-layout
1个回答
0
投票

您做对了。 ScrollView允许您滚动查看GridLayout中不适合ScrollView的部分。将size_hint设置为(1, .7)时,所有内容都适合ScrollView,因此它不会滚动。

您可以通过添加Widgets来占据更多空间来强制滚动(例如Labels无文本):

<Root>:
    ScrollView:
        size_hint: 1, .7
        GridLayout:
            size_hint_y: None
            cols: 1
            height: self.minimum_height
            Label:
                text: ''
                size_hint_y: None
                height: 300
            Button
                text: 'one'
                size_hint: 1, None
                height: self.texture_size[1]
            Button:
                text: 'two'
                size_hint: 1, None
                height: self.texture_size[1]
            Button:
                text: 'three'
                size_hint: 1, None
                height: self.texture_size[1]
            Button:
                text: 'four'
                size_hint: 1, None
                height: self.texture_size[1]
            Label:
                text: ''
                size_hint_y: None
                height: 300
© www.soinside.com 2019 - 2024. All rights reserved.