python - 用户登录后如何更新 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}
解决方案
在build()
您的方法中App
,该行:
self.content = ContentNavigationDrawer()
正在创建 的新实例ContentNavigationDrawer
,但这个新实例不是出现在您的 GUI 中的实例。您需要获取对ContentNavigationDrawer
GUI 中实际实例的引用。为此,您可以id
在kv
:
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
推荐阅读
- docker - 无法在裸机 kubernetes 集群上获取外部 IP
- javascript - 如何传递我的模板文字之外的变量?
- tfs - TFS On Prem 维护模式
- utf-8 - 不懂将 ANSI 编码为 UTF8
- linked-list - 没有 malloc 的 no_std 中的无堆链表
- ios - 使用 formSheet 呈现样式调整已经呈现的模式视图控制器的大小
- python - ValueError:无法将大小为 200 的数组重塑为形状 (200,320,240,1)
- python - 需要帮助修复错误“TypeError:'NoneType' 对象不可下标”
- python - 当我输入“python3.6 get-pip.py”时,无法修复“zipimport.ZipImportError:无法解压缩数据;zlib 不可用”
- excel - 如何根据唯一 ID 重建数据集