【问题标题】:ScrollView not allowing me to scrollScrollView 不允许我滚动
【发布时间】:2021-11-07 18:37:48
【问题描述】:

我一直在开发我的第一个 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.x kivy scrollview


    【解决方案1】:

    您必须设置subgridheight。默认为size_hint=(1,1),这使得subgridScrollView 的大小相同,因此没有什么可以滚动的。这是您的代码的修改版本:

    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"
    

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

    【讨论】:

    • 非常感谢,约翰!!!它似乎让我现在滚动!但是,我不想再打扰你了,它只能让我滚动一点,它会一直回到列表的顶部。我怎样才能防止这种情况发生?我的 MovieList[0]["Box1"] 里面有 120 个标签,所以它肯定应该有空间来正确滚动
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多