繁体   English   中英

kivy 滚动视图卡住

[英]kivy scrollview gets stuck

我的 Kivy 应用程序中有一个滚动视图 object。 滚动视图包含一个 boxlayout,其中包含一定数量的图像。 在运行时,更多的图像被添加到这个 boxlayout 中。 目前,我尝试通过更改 scroll_y 属性来调整滚动视图以适应更改,并且效果很好。 在某些时候它卡住了(大约在第 225 张图像上),我不知道如何解决这个问题。 如果更改,Kivy 是否有办法自动重新调整滚动视图? 还是有比我做的更好的方法来解决这个问题?

这就是我到目前为止在 python (在滚动视图类中)所做的事情:

   def on_scroll_y(self, instance, scroll_val):
        global main_screen, generate_id
        if scroll_val < get_scroll_distance() * scrolls_to_another_page:
            box = main_screen.ids.notebook_image # this is the boxlayout that holds the images
            new_image = MyImage() # just an image class
            new_image.id = next(generate_id)
            box.add_widget(new_image)
            self.scroll_y = new_image.height / box.height # this is my try to adjust the scroll value

这就是它在 kv 文件中的定义方式:

           MyScrollView:
                bar_color: [1, 0, 0, 1]
                id: notebook_scroll
                padding: 0
                spacing: 0
                do_scroll: (False, True)  # up and down
                BoxLayout:
                    padding: 0
                    spacing: 0
                    orientation: 'vertical'
                    id: notebook_image
                    size_hint: 1, None
                    height: self.minimum_height
                    MyImage:

<MyImage>:
    source: 'images/notebook2.png'
    allow_stretch: True
    keep_ratio: False
    size: root.get_size_for_notebook() #not important for this case, just screen size manipulation
    size_hint: None, None

当我回答你关于这个主题的原始问题时,我使用了测试:

 if scroll_val < 0.05:

但是,我认为这是一个糟糕的选择。 我的测试(以及您的测试)的问题在于,随着box.height变大(添加了Images ),计算出的新scroll_y变得越来越小。 一旦新的scroll_y小于测试值(在我的情况下0.05或在您的情况下get_scroll_distance() * scrolls_to_another_page ),然后设置新的scroll_y值会再次触发on_scroll_y()方法。 这可能导致无限循环。 我认为最好的选择是实际测试是否等于0.0 在我所有的计算机科学生涯中,我一直避免测试浮点值的相等性,但我相信这是一个例外。 因此,我建议将您的测试更改为:

 if scroll_val == 0.0:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM