【问题标题】:Kivy ScrollView won't scroll to lower contentKivy ScrollView 不会滚动到较低的内容
【发布时间】:2021-12-08 16:42:50
【问题描述】:

我有一个使用 AsyncImage 对象填充的 ScrollView。但是,我的 ScrollView 似乎认为没有什么可以向下滚动,并将其视为过度滚动。我认为这是因为我在实例化后添加到它的子布局,但不知道如何修复它。

错误行为:https://i.imgur.com/OjcR2RY.mp4

现在,我已经考虑过完全禁用过度滚动行为,但我需要它才能正常工作以用于未来的功能。

main.py

import gc

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import AsyncImage
from kivy.uix.widget import Widget


class ProviderWindow(Widget):

    def __init__(self, **kwargs):
        super(ProviderWindow, self).__init__(**kwargs)
        self.C_L = GridLayout(cols=3, spacing=0, size_hint=(1, None), pos=(0, 0))
    
    def search(self):

        # Clear any existing images inside the view when starting a fresh search
        for child in App.get_running_app().root.ids.image_scroll_view.children:
            del child
        App.get_running_app().root.ids.image_scroll_view.clear_widgets()
        gc.collect(generation=2)

        # GridView properties
        self.C_L.col_default_width = 500
        self.C_L.row_default_height = 500

        urls = self.gb.search()  # Get a list of URLs to load into AsyncImages (can simply be a list of Strings)

        # Adding All images to GridLayout
        for entry in urls:
            img = AsyncImage(source=entry, keep_ratio=True, allow_stretch=True)
            img.size_hint = (1, 1)
            self.C_L.add_widget(img)

        # Adding GridLayout to ScrollView
        self.ids.image_scroll_view.add_widget(self.C_L)


class SimpleApp(App):
    pass

简单.kv

ProviderWindow:

<ProviderWindow>:
    BoxLayout:
        orientation: "vertical"
        size_hint: None, None
        size: (root.size[0], root.size[1])

        BoxLayout:
            orientation: "horizontal"
            size_hint: 1, 0.1

            Button:
                id:search_button
                text:"Search"
                on_press: root.search()
                size_hint: 0.3,None

            TextInput
                id:tags
                size_hint: 0.4, None

            Button:
                id:search_button
                text:"More"
                size_hint: 0.3,None

            Button:
                id:settings_button
                text:"Settings"
                size_hint: 0.2, None


        ScrollView:
            id: image_scroll_view
            do_scroll_x: False
            do_scroll_y: True

【问题讨论】:

    标签: python user-interface kivy


    【解决方案1】:

    当您使用 GridLayoutBoxLayout 时,您应该指定那里的高度,否则它们只会采用父尺寸

    但是当我们在ScrollView 小部件中使用它时,您应该使用minimum_height 使高度等于GridLayout 子级

    在你的情况下,解决方案是

    self.C_L = GridLayout(cols=3, spacing=0, size_hint=(1, None), pos=(0, 0),height=self.minimum_height)
    
    

    【讨论】:

    • 这样做会导致AttributeError: 'ProviderWindow' object has no attribute 'minimum_height'
    【解决方案2】:

    我必须根据 GridLayout 的内容设置高度。

    我通过添加实现了这一点

    self.C_L.bind(minimum_height=self.C_L.setter('height'))

    工作就像一个魅力。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 2013-04-13
      • 1970-01-01
      相关资源
      最近更新 更多