Kivy: cant get scroll_to in Scrollview working

167 Views Asked by At

Trying code like this I get the Scrollview to work nicely but the scroll_to() function doesn't do anything. The view still starts at the top position.

pixperhour = 60
tagHL = []

dayview = ScrollView(size_hint=(1,1))
test = BoxLayout(size_hint=(1,None), height=24*pixperhour, orientation='vertical')
dayview.add_widget(test)

for i in range(24):
    tagHL.append(Label())
    test.add_widget(tagHL[i])
    tagHL[i].text = "Label "+str(i)
    tagHL[i].color = (1, 1, 1)
    tagHL[i].font_size = 30

dayview.scroll_to(tagHL[12])

I would expect the viewport of the scrollview to change so that the label tagHL[12] becomes visible. Am I doing it wrong or is the function bugged?

1

There are 1 best solutions below

8
ApuCoder On BEST ANSWER

In order to make it happen as soon as the widget creation is done you can schedule it after (or, before) some (or, no) time as, Clock.schedule_once(lambda *args : dayview.scroll_to(tagHL[12]),dt) where dt can be -1, 0 or any positive value.

As a side note, if you are using tagHL just as a widget container then it will better to use the children attribute of the BoxLayout, test. So, that will be like,

    for i in range(24):
        self.lbl = Label(
            text=f"Label {i}",
            color = [1, 1, 1],
            font_size = 30,
        )
        test.add_widget(self.lbl)
    Clock.schedule_once(lambda *x : dayview.scroll_to(test.children[::-1][12]))