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

Python Kivy添加Scroll标签后界面无内容显示问题求助

问题原因
  1. 根组件加载错误:你的kv文件将Scroll作为根组件直接加载,你自定义的main_widget等其他组件完全没有被加入到视图树中,自然无法显示。
  2. ScrollView配置错误:Kivy的ScrollView仅允许包含一个子控件,你自定义的<Scroll@ScrollView>规则仅声明了stac属性,没有将stac组件作为唯一子控件添加到ScrollView中,ScrollView本身是空的,没有可显示内容。
  3. 依赖导入缺失:Python代码中ScrollView的导入语句被注释,自定义Scroll组件找不到父类,运行时会存在隐性异常。
  4. stac高度配置错误:你将stac的高度写死为400,而内部有100个100dp高的按钮,内容总高度远大于400,即使添加到ScrollView中也无法正常滚动。
解决步骤
  • 第一步:取消Python代码中ScrollView导入的注释,导入所需依赖。
  • 第二步:调整kv文件结构,使用垂直BoxLayout作为根组件,将ScrollView和你需要显示的其他组件(比如main_widget)都加入到根布局中,保证所有内容都能被渲染。
  • 第三步:修正ScrollView的规则,将stac作为子组件添加,同时将stac的height绑定为自身的minimum_height,实现高度自适应内容,保证滚动效果正常。
  • 第四步:修复stac的高度配置,删除写死的height=400,改为动态绑定。
修正后代码示例

kv文件代码

<Scroll@ScrollView>:
    stac:stac_id
    Stac:
        id:stac_id
        size_hint:1,None
        height:self.minimum_height

<Stac@StackLayout>:
    #left-right top-bottom
    #horizontal
    #orientation:"lr-bt"
    #padding:("200dp","200dp","100dp","200dp")
    #spacing:"20dp","20dp"

<grid@GridLayout>:
#rows
#columns
    rows:2
    Button:
        text: "A"
        size_hint:None,.1
        width:"60dp"
    Button:
        text: "b"
    boxlay:
        text: "c"

    Button:
        text: "d"
        #size_hint:.5,.9
    Button:
        text: "e"
    Button:
        text: "f"

<anc>:
#right,left,center
#bottom,top,center
    anchor_x:"right"
    anchor_y:"bottom"
    BoxLayout:
        size_hint:.2,.2
        Button:
            text: "A"
            #size_hint:.2,.2
            #x,center_x,right
            #y,center_y,top
            pos_hint:{"right":.8}
            spacing:"10dp"

        Button:
            text: "B"
           # size_hint:.2,.2
            #x,center_x,right
            #y,center_y,top
            pos_hint:{"right":.3}
            spacing:"10dp"


<boxlay>:
    #horizontal
    #orientation:'vertical'
    Button:
        text: "A"
        size_hint:.2,.2
        #x,center_x,right
        #y,center_y,top
        pos_hint:{"right":.8}
        spacing:"10dp"
    Button:
        text: "A"

<main_widget>:
    Button:
        text:"hello"
        size:"100dp","80dp"
        pos:"100dp","200dp"
        color:1,1,0,1

    Label:
        text:"hello"
        size:"100dp","80dp"
        pos:"300dp","200dp"
        color:1,0,0,1

# 根布局
BoxLayout:
    orientation:'vertical'
    Scroll:
        size_hint:1,0.7
    main_widget:
        size_hint:1,0.3

Python文件代码

from kivy.app import App
from kivy.metrics import dp
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.stacklayout import StackLayout
from kivy.uix.widget import Widget


class Stac(StackLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        ##self.orientation="lr-bt"
        for i in range(0, 100):
            size = dp(100)
            b1 = Button(text=str(i + 1), size_hint=(None, None), size=(size, size))
            self.add_widget(b1)


"""class grid(GridLayout):
    pass"""


class anc(AnchorLayout):
    pass


class boxlay(BoxLayout):
    pass
    """
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        ##self.orientation='vertical'
        b1=Button(text="A",)
        b2 = Button(text="B")
        b3 = Button(text="c")
        self.add_widget(b1)
        self.add_widget(b2)
        self.add_widget(b3)
    """


class main_widget(Widget):
    pass


class myapp(App):
    pass


myapp().run()

注:代码中将自定义类名stac调整为Stac符合Python类名命名规范,不影响功能运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 20:15:04