Kivy MD:从屏幕类访问标签ID

时间:2019-12-15 11:48:04

标签: python kivy

我正在使用KivyMD,并希望使用“ on_pre_enter”方法重新刷新第二屏幕上的数据。 我正在尝试从屏幕类“ on_pre_enter”方法访问标签ID,但没有成功。

我能够从“ MainApp”类访问标签ID,但无法从“ Second_Screen”类访问标签ID。

MainApp.py

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen


class MainApp(MDApp):
    def __init__(self, **kwargs):
        self.title = "My Material Application"
        self.theme_cls.primary_palette = "Blue"
        super().__init__(**kwargs)

    def on_start(self):
        self.root.ids.main_screen_label.text = "Main Screen Updated"

    def build(self):
        self.root = Builder.load_file("app.kv")


class Second_Screen(Screen):
    def on_pre_enter(self):
        MainApp.ids.second_screen_label = "Second Screen Updated"

    pass


if __name__ == "__main__":
    MainApp().run()

App.kv

NavigationLayout:
    MDNavigationDrawer:
        NavigationDrawerSubheader:
            text: "Menu:"

        NavigationDrawerIconButton:
            icon:'battery'
            text: "Main Screen"
            on_release:
                screen_manager.current = "main_screen"

        NavigationDrawerIconButton:
            icon:'battery'
            text: "Second Screen"
            on_release:
                screen_manager.current = "second_screen"


    BoxLayout:
        id: box1
        orientation: 'vertical'
        MDToolbar:
            title: "My App"
            md_bg_color: app.theme_cls.primary_color
            left_action_items:
                [['menu', lambda x: app.root.toggle_nav_drawer()]]

        ScreenManager:
            id: screen_manager
            Screen:
                name: "main_screen"
                BoxLayout:
                    size_hint: .8, .8
                    pos_hint: {"center_x": .5, "center_y": .5}
                    spacing: dp(100)
                    orientation: 'vertical'


                    MDLabel:
                        id: main_screen_label
                        text: "Main Screen Default"

            Second_Screen:
                name: "second_screen"
                FloatLayout:
                    id: float
                    size_hint: .8, .8
                    pos_hint: {"center_x": .5, "center_y": .5}
                    spacing: dp(100)
                    orientation: 'vertical'


                    MDLabel:
                        id: second_screen_label
                        text: "Second Screen Default"

我也尝试使用:

class Second_Screen(Screen):
    def on_pre_enter(self):
        self.ids.second_screen_label.text = "Second Screen Updated"
    pass 

启动i之后,出现以下错误:

   self.ids.second_screen_label.text = "Second Screen Updated"
   File "kivy\properties.pyx", line 863, in 
   kivy.properties.ObservableDict.__getattr__
   AttributeError: 'super' object has no attribute '__getattr__'

从屏幕类访问KV文件中定义的ID的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

在单独的规则<MyScreen>中定义屏幕布局:

<MyScreen>:
    name: "my_screen"

    FloatLayout:
        # ...

要将此屏幕添加到ScreenManager,只需编写:

    ScreenManager:
        id: screen_manager

        MyScreen:

要访问屏幕小部件,请使用

  • 来自屏幕类别:self.ids.my_id
  • 来自应用类:self.root.ids.screen_manager.get_screen("screen_name").ids.my_id
  • 来自其他小部件:App.get_running_app()..root.ids.screen_manager.get_screen("screen_name").ids.my_id

因此,您的完整代码将是:

main.py

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen


class MainApp(MDApp):
    def __init__(self, **kwargs):
        self.title = "My Material Application"
        self.theme_cls.primary_palette = "Blue"
        super().__init__(**kwargs)

    def build(self):
        self.root = Builder.load_file("app.kv")

    def on_start(self):
        self.root.ids.screen_manager.get_screen(
            "main_screen"
        ).ids.main_screen_label.text = "Main Screen Updated"


class MainScreen(Screen):
    pass


class SecondScreen(Screen):
    def on_pre_enter(self):
        self.ids.second_screen_label.text = "Second Screen Updated"


if __name__ == "__main__":
    MainApp().run()

app.kv

NavigationLayout:
    MDNavigationDrawer:
        NavigationDrawerSubheader:
            text: "Menu:"

        NavigationDrawerIconButton:
            icon: "battery"
            text: "Main Screen"
            on_release:
                screen_manager.current = "main_screen"

        NavigationDrawerIconButton:
            icon: "battery"
            text: "Second Screen"
            on_release:
                screen_manager.current = "second_screen"


    BoxLayout:
        id: box1
        orientation: "vertical"
        MDToolbar:
            title: "My App"
            md_bg_color: app.theme_cls.primary_color
            left_action_items:
                [["menu", lambda x: app.root.toggle_nav_drawer()]]

        ScreenManager:
            id: screen_manager

            MainScreen:
            SecondScreen:

<MainScreen>:
    name: "main_screen"

    BoxLayout:
        size_hint: .8, .8
        pos_hint: {"center_x": .5, "center_y": .5}
        spacing: dp(100)
        orientation: "vertical"

        MDLabel:
            id: main_screen_label
            text: "Main Screen Default"


<SecondScreen>:
    name: "second_screen"

    FloatLayout:
        id: float
        size_hint: .8, .8
        pos_hint: {"center_x": .5, "center_y": .5}
        spacing: dp(100)
        orientation: "vertical"

        MDLabel:
            id: second_screen_label
            text: "Second Screen Default"