【问题标题】:Kivy scroll view issueKivy 滚动视图问题
【发布时间】:2020-03-08 23:20:25
【问题描述】:

我想使用kv 语言向radiobuttons 添加滚动视图。

我的代码:

main.py

from kivymd.app import MDApp
from kivy.lang import Builder

class MyKivyApp(MDApp):
    def build(self):
        self.root = Builder.load_file("main.kv")
        return self.root

if __name__ == "__main__":
    MyKivyApp().run()

ma​​in.kv

<RadioButton@CheckBox>:
    group: "radioButtons"
    size_hint_y: None
    height: 40

<TestLabel@Label>:
    text: "Test"
    color: (1, 0, 0, 1)

ScrollView:
    size_hint: (1, 1)
    pos_hint: {"x": 0.45}

    GridLayout:
        cols: 1
        padding: 10
        spacing: 15
        size_hint_y: None
        size_hint_x: None
        width: dp(100)
        height: self.minimum_height

        TestLabel:
            text: "Main Label"

        TextInput:
            id: oneTxt
            pos_hint: {"top": 0.1}
            size_hint: (1, 1)

        Button:
            id: btn1
            text: "Button 1"
            pos_hint: {"top": 0.4}

        GridLayout:
            cols: 2
            padding: 0
            spacing: 0
            size_hint_y: None
            size_hint_x: None
            height: self.minimum_height

            RadioButton:
            TestLabel:
                text: "Test1"

            RadioButton:
            TestLabel:
                text: "Test2"

            RadioButton:
            TestLabel:
                text: "Test3"

            RadioButton:
            TestLabel:
                text: "Test4"

            RadioButton:
            TestLabel:
                text: "Test5"

            RadioButton:
            TestLabel:
                text: "Test6"

            RadioButton:
            TestLabel:
                text: "Test7"

            RadioButton:
            TestLabel:
                text: "Test8"

            RadioButton:
            TestLabel:
                text: "Test9"

            RadioButton:
            TestLabel:
                text: "Test10"

            RadioButton:
            TestLabel:
                text: "Test11"

            RadioButton:
            TestLabel:
                text: "Test12"

            RadioButton:
            TestLabel:
                text: "Test13"

            RadioButton:
            TestLabel:
                text: "Test14"

            RadioButton:
            TestLabel:
                text: "Test15"

            RadioButton:
            TestLabel:
                text: "Test16"

            RadioButton:
            TestLabel:
                text: "Test17"

            RadioButton:
            TestLabel:
                text: "Test18"

            RadioButton:
            TestLabel:
                text: "Test19"

            RadioButton:
            TestLabel:
                text: "Test20"

            RadioButton:
            TestLabel:
                text: "Test21"

            RadioButton:
            TestLabel:
                text: "Test22"

            RadioButton:
            TestLabel:
                text: "Test23"

            RadioButton:
            TestLabel:
                text: "Test24"

            RadioButton:
            TestLabel:
                text: "Test25"

            RadioButton:
            TestLabel:
                text: "Test26"

            RadioButton:
            TestLabel:
                text: "Test27"

            RadioButton:
            TestLabel:
                text: "Test28"

            RadioButton:
            TestLabel:
                text: "Test29"

            RadioButton:
            TestLabel:
                text: "Test30"

            RadioButton:
            TestLabel:
                text: "Test31"

            RadioButton:
            TestLabel:
                text: "Test32"

        TestLabel:
            text: "End Label"

        Button:
            id: btn2
            text: "Button 2"
            pos_hint: {"top": 0.8}

滚动有效,但其他小部件很小并且相互重叠。查看下图:

如何解决?谢谢。

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    当您将GridLayoutheight 设置为self.minimum_height 时,您需要为每个孩子指定一个height。例如,外部GridLayout 的前三个孩子可能是:

        TestLabel:
            text: "Main Label"
            size_hint: (None, None)
            size: self.texture_size[0] + 10, self.texture_size[1] + 10
    
        TextInput:
            id: oneTxt
            size_hint: (1, None)
            height: 40
    
        Button:
            id: btn1
            text: "Button 1"
            size_hint: (None, None)
            size: self.texture_size[0] + 10, self.texture_size[1] + 10
    

    同样,最后两个可能是:

        TestLabel:
            text: "End Label"
            size_hint: (None, None)
            size: self.texture_size[0] + 10, self.texture_size[1] + 10
    
        Button:
            id: btn2
            text: "Button 2"
            size_hint: (None, None)
            padding: 10,10
            size: self.texture_size
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多