Kivys GridLayou中的滚动视图

2024-10-03 19:20:06 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我应用程序的kv文件

<myAppLayout>       
    canvas.before:
        Color:
            rgba:1, 1, 1,1
        Rectangle:
            pos: self.pos
            size: self.size

    ScrollView:
        size: self.size 
        size_hint: (None, None)
        GridLayout:
            cols:1      

            TextInput:      

                pos_hint:{"center_x":0.5,"y":0.1}
                color:0,0.5,1,1
                background_color:0,0.5,1,1
                size:20,20          


            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size
            Label:
                color:1,0,1,1
                text:"hello"
                text_size:self.size

Python代码如下所示

class myAppLayout(GridLayout):
    pass

问题是我的输出看起来像这样(左下角),而我希望所有的东西都按照设备的大小逐行排列 enter image description here


Tags: 文件textposselfnone应用程序hellosize
1条回答
网友
1楼 · 发布于 2024-10-03 19:20:06

如果希望ScrollView的内容占用所有可用空间,则不能执行以下操作:

size: self.size 
size_hint: (None, None)

另一方面,ScrollView中的小部件必须有一个定义的高度(size\u hint\u y=None),否则它们将根据ScrollView的大小自动调整大小。你知道吗

请记住,如果不指定列或行,GridLayout将引发异常。必须为myAppLayout分配若干行或列。你知道吗

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder

kv_text = '''

<MyLabel@Label>:
    color:1,0,1,1
    text:"hello"
    text_size:self.size
    size_hint_y:None
    height: 20

<myAppLayout>
    cols: 1
    canvas.before:
        Color:
            rgba:1, 1, 1,1
        Rectangle:
            pos: root.pos
            size: root.size

    ScrollView:
        GridLayout:
            cols:1
            size_hint_y: None 
            height: self.minimum_height   

            TextInput:      
                pos_hint:{"center_x":0.5,"y":0.1}
                color:0,0.5,1,1
                background_color:0,0.5,1,1
                size_hint_y: None
                height: 30          

            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:
            MyLabel:

'''

class myAppLayout(GridLayout):
    pass

class MyApp(App):
    def build(self):
        return myAppLayout()

def main():
    Builder.load_string(kv_text)
    app = MyApp()
    app.run()

if __name__ == '__main__':
    main()

输出:

enter image description here

相关问题 更多 >