简体   繁体   English

滚动在 kivy 按钮布局中不起作用

[英]Scroll does not work in kivy Layout of buttons

<Builder_Screen>
    ScrollView:
        do_scroll_y:True
        FloatLayout:
            Button:
                text:"Heading"
                size_hint_x: .25
                size_hint_y: .25
                text_size: self.size
                halign:"center"
                valign:"center"
                pos: 0,10
            Button:
                text:"Paragraph"
                halign:"center"
                valign:"center"
                size_hint_x: .25
                size_hint_y: .25
                pos: (0,self.height)

I have some buttons like these with positions like pos: 0, self.height 2, self.height 3 etc. But, the scroll layout does not work as intended.我有一些像这样的按钮,位置像 pos: 0, self.height 2, self.height 3 等。但是,滚动布局不能按预期工作。 Can you help me regarding that....你能帮我解决这个问题吗......

According to the documentation , regarding the child of a ScrollView :根据文档,关于ScrollView的孩子:

By default, the size_hint is (1, 1), so the content size will fit your ScrollView exactly (you will have nothing to scroll).默认情况下,size_hint 为 (1, 1),因此内容大小将完全适合您的 ScrollView(您将无法滚动)。 You must deactivate at least one of the size_hint instructions (x or y) of the child to enable scrolling.您必须至少停用子项的 size_hint 指令(x 或 y)之一才能启用滚动。

So, you probably need to set size_hint_y: None for your FloatLayout .因此,您可能需要为您的FloatLayout设置size_hint_y: None That will cause further problems, because you have size_hint_y values for your Buttons .这将导致进一步的问题,因为您的Buttons具有size_hint_y值。 You can set the heights of your Buttons and eliminate the size_hint_y values.您可以设置Buttons的高度并消除size_hint_y值。 Another approach would be to use a GridLayout :另一种方法是使用GridLayout

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen

kv = '''
<Builder_Screen>
    ScrollView:
        do_scroll_y:True
        effect_cls: 'ScrollEffect'
        GridLayout:
            cols: 2
            size_hint_y: None
            height: self.minimum_height
            padding: 5
            spacing: 5
            Button:
                text:"Heading1"
                halign:"center"
                valign:"center"
                size_hint_y: None
                height: 48
            Button:
                text:"Paragraph1"
                halign:"center"
                valign:"center"
                size_hint_y: None
                height: 48
            Button:
                text:"Heading2"
                halign:"center"
                valign:"center"
                size_hint_y: None
                height: 48
            Button:
                text:"Paragraph2"
                halign:"center"
                valign:"center"
                size_hint_y: None
                height: 48
            Button:
                text:"Heading3"
                halign:"center"
                valign:"center"
                size_hint_y: None
                height: 48
            Button:
                text:"Paragraph3"
                halign:"center"
                valign:"center"
                size_hint_y: None
                height: 48
'''

class Builder_Screen(Screen):
    pass

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

TestApp().run()

Note that every Button has an explicit height .请注意,每个Button都有一个明确的height That is required if you use height: self.minimum_height for their container.如果您使用height: self.minimum_height作为他们的容器,这是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM