Scroll_to() Kivy

问题描述 投票:0回答:1

我正在尝试创建一个在 ScrollView 内的多个标签中包含大量文本的应用程序。此外,我使用带有按钮的 MDNavigationDrawer 来自动滚动到这些标签,而不是让用户手动滚动。我不断收到错误消息“UniversalPatientCareL1”未定义。我试图改变层次结构但无济于事,我不知所措。我使用的代码如下:

<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)
python kivy hierarchy scrollto
1个回答
0
投票

您收到的错误是由于您的标签的名称和 ID 与您尝试在 on_release 事件中使用的名称不匹配造成的。设置名称时应不带引号,并且使用 id 时应不带引号。这是更正后的代码:

<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)

© www.soinside.com 2019 - 2024. All rights reserved.