You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kivy新手求助:ScrollView嵌套GridLayout滚动失效及布局错位问题

Kivy GridLayout 滚动问题解决方案

问题根源分析

你的代码里有几个关键错误导致滚动失效或布局异常:

  • 拼写错误:heigth应该是height,导致self.minimum_height无法生效,子GridLayout高度没正确计算
  • 固定pos滥用:所有子元素用了固定坐标(比如pos:5,-210),这会让元素脱离布局控制,滚动时不会跟随移动
  • 子GridLayout属性设置错误:仅设size_hint_y:0但高度计算失效,无法触发滚动

修正步骤

  1. 修正拼写错误
    将子GridLayout里的heigth:self.minimum_height改为height:self.minimum_height

  2. 正确配置滚动布局
    ScrollView的子控件必须满足:

    • size_hint_y: None(或0,让高度由height属性控制)
    • height: self.minimum_height(自动计算所有子元素的总高度)
      当这个总高度超过ScrollView的高度时,滚动功能自动激活
  3. 移除固定pos,改用布局属性控制位置
    替换所有pos固定值,用padding、spacing、size_hint、rows/cols等布局属性来排列元素,确保滚动时元素能正常跟随

修改后的示例代码

<layout_>:
    id: layout
    GridLayout:
        cols: 1
        ScrollView:
            size: 300, 500                
            pos: 0, 0
            do_scroll_y: True
            do_scroll_x: False
            size_hint: (None, None)

            GridLayout:
                size_hint_y: None
                height: self.minimum_height
                cols: 1
                padding: 5
                spacing: 10

                # 第一个卡片
                GridLayout:
                    size_hint_y: None
                    height: 100
                    canvas:
                        Color:
                            rgba: 1, 1, 1, .5
                        RoundedRectangle:
                            pos: self.pos
                            size: self.size

                # 第二个卡片
                GridLayout:
                    size_hint_y: None
                    height: 250
                    canvas:
                        Color:
                            rgba: 1, 1, 1, .5
                        RoundedRectangle:
                            pos: self.pos
                            size: self.size

                # 第三个卡片
                GridLayout:
                    size_hint_y: None
                    height: 100
                    canvas:
                        Color:
                            rgba: 1, 1, 1, .5
                        RoundedRectangle:
                            pos: self.pos
                            size: self.size

                # 第四个卡片
                GridLayout:
                    size_hint_y: None
                    height: 100
                    canvas:
                        Color:
                            rgba: 1, 1, 1, .5
                        RoundedRectangle:
                            pos: self.pos
                            size: self.size

                # 温度和地区标签
                GridLayout:
                    size_hint_y: None
                    height: 150
                    spacing: 0
                    padding: 0

                    Label:               
                        text: "13\n yourcounty"
                        font_size: 30
                        size_hint_x: 1

                    Label:
                        text: "feels like"
                        font_size: 8
                        size_hint_x: 1

                # 横向滚动的子ScrollView
                ScrollView:
                    size_hint_y: None
                    height: 100
                    do_scroll_x: True
                    do_scroll_y: False
                    GridLayout:
                        id: set_and_rise_grid
                        size_hint_x: None
                        width: self.minimum_width
                        height: self.minimum_height
                        # 这里可以添加横向滚动的内容

额外说明

  • 给滚动的GridLayout设置cols:1,确保子元素垂直排列
  • 每个子GridLayout设置size_hint_y: None和固定height,让父GridLayout能正确计算总高度
  • 把RoundedRectangle的pos和size绑定到自身布局的self.pos和self.size,避免固定坐标导致的错位

内容的提问来源于stack exchange,提问作者Plisok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 20:17:34