如何为 MDLabel 中的文本使用变量?
Posted
技术标签:
【中文标题】如何为 MDLabel 中的文本使用变量?【英文标题】:How do I use a variable for the text in an MDLabel? 【发布时间】:2021-04-22 11:57:42 【问题描述】:我正在尝试将我从 MDTextField 输入的用户名放入 MDLabel 文本中。在我的例子中,MDLabel 位于用户按下提交按钮并进入下一个屏幕后出现的 MDNavigationDrawer 中。
目前我能想到的只是在提交屏幕中将 usn 声明为字符串属性:
class SubmitScreen(Screen):
usn = StringProperty()
然后创建了一个方法来将 usn 设置为我的应用程序中 MDTextfield 的输入:
def getname(self):
usn = (self.root.get_screen("menu").ids.userdata.text)
然后我将 MDLabel 中的文本设置为 root.usn
MDLabel:
text: root.usn
font_style: 'Subtitle1'
size_hint_y:None
height: self.texture_size[1]
当我进入下一个屏幕时,MDLabel 是空白的。它没有给我一个错误,但它没有显示我在 MDTextField 中输入的内容,它只是空白。
这是我的完整代码:
screen_helper = """
ScreenManager:
MenuScreen:
SubmitScreen:
<MenuScreen>:
name: 'menu'
MDTextField:
id : userdata
hint_text: "Enter Username"
helper_text: "or click on forgot username"
#helper_text_mode: "persistent"
helper_text_mode: "on_focus"
icon_right: "account"
icon_right_color: app.theme_cls.primary_color
pos_hint : 'center_x': 0.5, 'center_y': 0.4
size_hint_x:None
width:250
mode : "rectangle"
MDTextField:
id: userpass
hint_text: "Enter Password"
helper_text: "or click on forgot Password"
#helper_text_mode: "persistent"
password : True
helper_text_mode: "on_focus"
icon_right: "eye-off"
icon_right_color: app.theme_cls.primary_color
pos_hint : 'center_x': 0.5, 'center_y': 0.3
size_hint_x:None
width:250
mode : "rectangle"
MDLabel:
text: "Company X"
pos_hint: 'center_x':0.5, 'center_y':0.7
font_style: 'H3'
halign: 'center'
MDRectangleFlatButton:
text: 'Submit'
pos_hint: 'center_x':0.5, 'center_y':0.2
font_size : 20
on_press:
app.getname()
app.root.current = 'submit'
<SubmitScreen>:
name: 'submit'
NavigationLayout:
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Company X"
left_action_items: [["menu", lambda x : nav_drawer.toggle_nav_drawer()]]
elevation: 11
Widget:
MDFillRoundFlatIconButton:
spacing: '8dp'
padding: '20dp'
text: 'Menu'
pos_hint: 'center_x':0.5, 'center_y':0.9
icon: 'account-child'
on_press: root.manager.current = 'menu'
MDFillRoundFlatIconButton:
text: 'View Account'
pos_hint: 'center_x':0.5, 'center_y':1
font_size : 20
icon: 'account-cash'
on_press:
MDBottomAppBar:
md_bg_color: 0, 1, 0, 1
MDToolbar:
title: "by Tech Company"
left_action_items: [["coffee", lambda x : app.navigation_draw0()]]
right_action_items: [["clock", lambda x : app.navigation_draw1()]]
mode: 'free-end'
elevation: 13
type: 'bottom'
icon: 'account-check'
MDNavigationDrawer:
id: nav_drawer
BoxLayout:
orientation: 'vertical'
spacing: '8dp'
padding: '12dp'
Image:
source: 'img.jpg'
MDLabel:
text: root.usn
font_style: 'Subtitle1'
size_hint_y:None
height: self.texture_size[1]
MDLabel:
text: 'email@gmail.com'
font_style: 'Caption'
size_hint_y:None
height: self.texture_size[1]
ScrollView:
MDList:
#OneLineListItem:
OneLineIconListItem:
text: 'Profile'
IconLeftWidget:
icon: 'face-profile'
OneLineIconListItem:
text: 'Upload'
IconLeftWidget:
icon: 'upload'
OneLineIconListItem:
text: 'Logout'
conLeftWidget:
icon: 'logout'
MDRectangleFlatButton:
text: 'Menu'
pos_hint: 'center_x':0.5, 'center_y':0.4
on_press: root.manager.current = 'menu'
MDRectangleFlatButton:
text: 'View Account'
pos_hint: 'center_x':0.5, 'center_y':0.3
on_press:
"""
class MenuScreen(Screen):
pass
class SubmitScreen(Screen):
usn = StringProperty()
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SubmitScreen(name='submit'))
sm.add_widget(UploadScreen(name='upload'))
sm.add_widget(AccountScreen(name='account'))
class DemoApp(MDApp):
def build(self):
screen = Builder.load_string(screen_helper)
self.theme_cls.primary_palette = "Purple"
self.theme_cls.accent_palette = 'Blue'
self.theme_cls.primary_hue = "200"
self.theme_cls.theme_style = "Dark"
return screen
def getname(self):
usn = (self.root.get_screen("menu").ids.userdata.text)
DemoApp().run()
【问题讨论】:
【参考方案1】:为 MDLabel 设置一个 ID,然后在 getname 函数中将 MDLabel 文本更改为 MDTextField 的输入。
千伏:
MDLabel:
text: "Text before"
id: label
在python中:
def getname(self):
usn = (self.root.get_screen("menu").ids.userdata.text)
self.root.ids.label.text = usn
【讨论】:
以上是关于如何为 MDLabel 中的文本使用变量?的主要内容,如果未能解决你的问题,请参考以下文章
如何为多个文本框使用一个绑定来分隔 WPF XAML 中的三位数字?