首页 > 解决方案 > ScrollView 不允许我滚动

问题描述

我一直在开发我的第一个 kivy 应用程序以了解和理解该语言。我目前坚持让我的 ScrollView 实际滚动。我查找了许多关于该主题的不同答案和视频。下面是我当前的 ScrollView 脚本。任何人都可以帮助我吗?

class SearchMovie(GridLayout):
    global MovieList
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.cols = 1
        
        
        
        
        scrolly = ScrollView(size_hint=(1, None), size=(Window.width, Window.height*.9))
        subgrid = GridLayout(cols=1, spacing=20)
        movlist = MovieList[0]["Box1"]
        nmovlist = []
        for i in movlist:
            nmovlist.append(i)
        nmovlist.sort()
        for i in nmovlist:
            movlab = Label(text=i)
            subgrid.add_widget(movlab)
        scrolly.add_widget(subgrid)
        self.add_widget(scrolly)
        
        
        self.goback = Button(text="Go Back", background_color =[0, 0, 1, 1], pos_hint={'bottom':1, 'center_x':1})
        self.goback.bind(on_press=self.go_back)
        self.add_widget(self.goback)
        
    def go_back(self, instance):
        MovieScript.screen_manager.current = "Main Page" 

任何帮助将不胜感激!

标签: python-3.xkivyscrollview

解决方案


您必须设置height. subgrid默认值为size_hint=(1,1),它subgrid与 的大小相同ScrollView,因此没有可滚动的内容。这是执行此操作的代码的修改版本:

class SearchMovie(GridLayout):
    global MovieList
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.cols = 1
        scrolly = ScrollView(size_hint=(1, None), size=(Window.width, Window.height*.9))
        subgrid = GridLayout(cols=1, spacing=20, size_hint_y=None)  # use size_hint_y here
        movlist = MovieList[0]["Box1"]
        nmovlist = []
        for i in movlist:
            nmovlist.append(i)
        nmovlist.sort()
        label_height = 40  # this will be the height of each Label
        total_height = 0  # this will be the total height of the subgrid
        for i in nmovlist:
            movlab = Label(text=i, size_hint_y=None, height=label_height)  # set height
            total_height += label_height + 20  # sum heights (plus spacing)
            subgrid.add_widget(movlab)
        subgrid.height = total_height  # set actual height of subgrid
        scrolly.add_widget(subgrid)
        self.add_widget(scrolly)
        
        
        self.goback = Button(text="Go Back", background_color =[0, 0, 1, 1], pos_hint={'bottom':1, 'center_x':1})
        self.goback.bind(on_press=self.go_back)
        self.add_widget(self.goback)
        
    def go_back(self, instance):
        MovieScript.screen_manager.current = "Main Page" 

另一种方法是使用 kivy 语言。例如,您可以为SearchMovie班级制定规则:

kv = '''
<SearchMovie>:
    cols: 1
    ScrollView:
        size_hint: 1, 0.9
        GridLayout:
            id: grid
            cols: 1
            size_hint_y: None
            height: self.minimum_height
    Button:
        size_hint: 1, 0.1
        text: "Go Back"
        background_color: [0, 0, 1, 1]
        pos_hint: {'bottom': 1, 'center_x': 1}
        on_press: root.go_back()
'''

那么你的实际SearchMovie课程可以是:

class SearchMovie(GridLayout):
    global MovieList

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        subgrid = self.ids.grid
        movlist = MovieList[0]["Box1"]
        nmovlist = []
        for i in movlist:
            nmovlist.append(str(i))
        nmovlist.sort()
        for i in nmovlist:
            movlab = Label(text=i, size_hint_y=None, height=40)
            subgrid.add_widget(movlab)

    def go_back(self, instance):
        MovieScript.screen_manager.current = "Main Page"

请确保在创建实例kv之前加载上述字符串。SearchMovie


推荐阅读