基维:将滚动视图移动到某个y轴

2024-10-16 17:15:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个基本的应用程序,我需要将我的ScrollView移动到某个y。 我使用scroll_to(),但这次,我需要移动到某个位置,计算多个信息。(我没有在the doc中找到它)

from kivy.lang import Builder
from kivymd.app import MDApp

KV = '''
BoxLayout:
    ScrollView:
        BoxLayout:
            orientation: "vertical"
            size_hint_y: None
            height: "900dp"
            MDLabel:
                text: "yolo"
                size_hint_y: None
                height: "300dp"
            MDLabel:
                text: "yulu"
            MDLabel:
                text: "yulu"
            MDLabel:
                text: "yulu"
            MDLabel:
                text: "yulu"
            MDLabel:
                text: "yulu"
            MDLabel:
                text: "yulu"
'''

class App(MDApp):
    def build(self):
        self.box = Builder.load_string(KV)
        return self.box

App().run()

Tags: textfromimportselfnonesizebuilderheight
1条回答
网友
1楼 · 发布于 2024-10-16 17:15:56

看起来您需要scroll_y参数。这是一个百分比值,指示滚动“向下”的距离

从文档中:

a Y scrolling value, between 0 and 1. If 0, the content’s bottom side will touch the bottom side of the ScrollView. If 1, the content’s top side will touch the top side.

https://kivy.org/doc/stable/api-kivy.uix.scrollview.html#kivy.uix.scrollview.ScrollView.scroll_y

因此,如果设置ScrollView.scroll_y = 0.5,滚动框将向下滚动一半

如果您知道滚动的距离(以像素为单位),则始终可以将上述内容与convert_distance_to_scroll(dx, dy)结合起来,(同样来自文档):

Converts a distance in pixels to a scroll distance, depending on the content size and the scrollview size.

https://kivy.org/doc/stable/api-kivy.uix.scrollview.html#kivy.uix.scrollview.ScrollView.convert_distance_to_scroll

相关问题 更多 >