Scroll_to() Kivy

33 Views Asked by At

I am trying to create an app that contains a large amount of text within multiple Labels within a ScrollView. In addition, I am using an MDNavigationDrawer with buttons to auto-scroll to these Labels instead of making the user manually Scroll. I keep getting the error message "UniversalPatientCareL1" is not defined. I have attempted to alter the hierarchy to no avail and I am at a loss. The code I am using is as follows:

<Demo>
    Screen:
        MDBoxLayout:
            id: MainLayout
            orientation:'vertical'
            MDTopAppBar:
                pos_hint: {"top":1}
                title: "Navigation"
                left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]

        ScrollView:
            id: MyScrollView

            GridLayout:
                id: MyGridLayout
                cols:1
                size_hint_y:None
                size_hint:1,None
                height:self.minimum_height

                Label:
                    name: 'UniversalPatientCareL1'
                    id: 'UniversalPatientCareL1'
                    canvas.before:
                        Color:
                            rgba:(0,0,0,1)
                        Rectangle:
                            pos:self.pos
                            size:self.size
                    size_hint_y: None
                    height: self.texture_size[1]
                    text_size:self.width,None
                    text:'Universal Patient Care'
                    font_size:'52sp'

    MDNavigationDrawer:
        id:nav_drawer
        MDBoxLayout:
            orientation: 'vertical'

            ScrollView:
                GridLayout:
                    cols:1
                    size_hint_y:None
                    size_hint:1,None
                    height:self.minimum_height

                    Button:
                        name:"Universal"
                        canvas.before:
                            Color:
                                rgba:(21/255.0,113/255.0,146/255.0,1)
                            Rectangle:
                                pos:self.pos
                                size:self.size
                        size_hint_y: None
                        height: self.texture_size[1]
                        text_size:self.width,None
                        text:'Universal Patient Care'
                        font_size:'20sp'
                        on_release:MyScrollView.scroll_to(UniversalPatientCareL1)
1

There are 1 best solutions below

0
Elyas Creates On

The error you got is due to a mismatch between the name and id of your Label and the name you're trying to use in the on_release event. The name should be set without quotes, and the id should be used without quotes. Here is the corrected code:

<Demo>
    Screen:
        MDBoxLayout:
            id: MainLayout
            orientation: 'vertical'
            MDTopAppBar:
                pos_hint: {"top": 1}
                title: "Navigation"
                left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]

        ScrollView:
            id: MyScrollView

            GridLayout:
                id: MyGridLayout
                cols: 1
                size_hint_y: None
                size_hint: 1, None
                height: self.minimum_height

                Label:
                    name: UniversalPatientCareL1
                    id: UniversalPatientCareL1
                    canvas.before:
                        Color:
                            rgba: (0, 0, 0, 1)
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    size_hint_y: None
                    height: self.texture_size[1]
                    text_size: self.width, None
                    text: 'Universal Patient Care'
                    font_size: '52sp'

    MDNavigationDrawer:
        id: nav_drawer
        MDBoxLayout:
            orientation: 'vertical'

            ScrollView:
                GridLayout:
                    cols: 1
                    size_hint_y: None
                    size_hint: 1, None
                    height: self.minimum_height

                    Button:
                        name: Universal
                        canvas.before:
                            Color:
                                rgba: (21/255.0, 113/255.0, 146/255.0, 1)
                            Rectangle:
                                pos: self.pos
                                size: self.size
                        size_hint_y: None
                        height: self.texture_size[1]
                        text_size: self.width, None
                        text: 'Universal Patient Care'
                        font_size: '20sp'
                        on_release: MyScrollView.scroll_to(UniversalPatientCareL1)