首页 > 解决方案 > 用户登录后如何更新 KivyMD Nav Drawer?

问题描述

我想更新ContentNavigationDrawer显示用户照片和电子邮件的内容,但我似乎无法访问这些 ID。

在下面的login.py代码中,您可以看到我尝试使用一个简单的按钮来更新内容抽屉的代码。我错过了什么?

主文件

from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from test_login import LoginScreen

Window.size = (440, 760)
kv = '''
<ContentNavigationDrawer>:
    id: content_nav
    orientation: "vertical"
    AnchorLayout:
        anchor_x: "center"
        size_hint_y: None
        height: 100
        FitImage:
            id: user_photo
            radius: [50,]
            size_hint: None, None
            size: 100,100
            source: 'data/assets/img/profile_photo_placeholder.jpg'
    MDLabel:
        id: user_email
        text: "--"
        halign: "center"
        font_style: "Button"
        size_hint_y: None
        height: self.texture_size[1]
    ScrollView:
        MDList:
            OneLineIconListItem:
                text: 'Login'
                on_release:
                    root.nav_drawer.set_state("close")
                    root.screen_manager.current = "go_to_login"
Screen:
    MDToolbar:
        id: toolbar
        pos_hint: {"top": 1}
        left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]

    MDNavigationLayout:
        x: toolbar.height
        ScreenManager:
            id: screen_manager
            Screen:
                name: "words_nav_item"
            LoginScreen:
                name: "go_to_login"

        MDNavigationDrawer:
            id: nav_drawer
            ContentNavigationDrawer:
                screen_manager: screen_manager
                nav_drawer: nav_drawer

'''
class ContentNavigationDrawer(BoxLayout):
    pass

class main_test(MDApp):
    def build(self):
        self.content = ContentNavigationDrawer()
        self.kv = Builder.load_string(kv)
        return self.kv

main_test().run()

登录.py

from kivy.app import App
from kivy.lang import Builder
from kivymd.uix.screen import MDScreen

kv = '''
<LoginScreen>:
    MDGridLayout:
        cols: 1
        padding: [50,40,50,0]
        MDGridLayout:
            cols: 1
            size_hint: 1, None
            height: 240
            padding: [0,0,0,20]
            AnchorLayout:
                anchor_x: 'center'
                anchor_x: 'center'
                anchor_y: 'center'
                MDRectangleFlatButton:
                    text: "Update User Profile "
                    on_release:
                        root.update_user_profile()

'''

class LoginScreen(MDScreen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.app = App.get_running_app()
        Builder.load_string(kv)

    def update_user_profile(self):
        user_photo = "data/img/profile.png"
        user_email = "user@mail.com"

        #NOT WORKING
        #self.app.content.ids.user_email.text = user_email

        # NOT WORKING
        #self.app.kv.ids.user_email.text = user_email

        # WORKING
        self.app.kv.ids.toolbar.pos_hint= {"top": .5}

标签: pythonpython-3.xkivykivy-languagekivymd

解决方案


build()您的方法中App,该行:

self.content = ContentNavigationDrawer()

正在创建 的新实例ContentNavigationDrawer,但这个新实例不是出现在您的 GUI 中的实例。您需要获取对ContentNavigationDrawerGUI 中实际实例的引用。为此,您可以idkv:

    MDNavigationDrawer:
        id: nav_drawer
        ContentNavigationDrawer:
            id: content  # new id to access this instance
            screen_manager: screen_manager
            nav_drawer: nav_drawer

然后你可以id在你的build()方法中使用这个新的:

class main_test(MDApp):
    def build(self):
        # self.content = ContentNavigationDrawer()
        self.kv = Builder.load_string(kv)
        self.content = self.kv.ids.content
        return self.kv

然后,在您的update_user_profile()方法中,该行:

self.app.content.ids.user_email.text = user_email

应该管用。和行:

self.app.kv.ids.user_email.text = user_email

可以修改为使用新的id

self.app.kv.ids.content.ids.user_email.text = user_email

推荐阅读