You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KivyMD开发求助:如何在ScreenDisplayLabel中展示指定变量文本

解决KivyMD跨屏幕传递并显示变量内容的问题

问题根源

  • value_to_display_1和value_to_display_2都是局部变量,方法执行完毕后就会被销毁,其他方法或页面无法访问
  • ScreenDisplayLabel中没有添加任何用于显示文本的控件
  • Extract_Value_Login方法从未被调用,无法获取其中的value_to_display_2值
  • 当前逻辑无论登录是否成功,都会直接切换到显示页面

修正后的完整代码

from __future__ import print_function
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from plyer import notification

KV = """
WindowManager:
    ScreenLogin:
        id: ScreenLogin
    ScreenDisplayLabel:
        id: ScreenDisplayLabel

<ScreenLogin>:
    name: "ScreenLogin"
    FloatLayout:
        canvas.before:
            Color:
                rgba: 1, 1, 1, 1
            Rectangle:
                pos: self.pos
                size: self.size
        GridLayout:
            cols: 1
            spacing: 5
            size_hint: 0.8, .6
            pos_hint: {"center_x": .5, "center_y": .4}
            Label:
                text: "User"
                size_hint_x: .3
                text_size: self.size
                halign: 'center'
                valign: 'middle'
                color: 0,0,0,1
                allow_copy: True
            TextInput:
                id: user
                multiline: False
                halign: 'center'
                focus: True   

            Label:
                text: "Password"
                size_hint_x: .3
                text_size: self.size
                halign: 'center'
                valign: 'middle'
                color: 0,0,0,1
            TextInput:
                id: password
                multiline: False
                password: True
                password_mask: '#'
                halign: 'center'
            Button:
                size_hint: 1., 1.
                text: "Login"
                on_release:
                    app.login()  # 先执行登录验证,内部处理页面切换

<ScreenDisplayLabel>:
    name: "ScreenDisplayLabel"
    FloatLayout:
        canvas.before:
            Color:
                rgba: 1, 1, 1, 1
            Rectangle:
                pos: self.pos
                size: self.size
        # 添加两个用于显示内容的标签
        Label:
            id: label1
            text: ""
            text_size: self.size
            halign: 'center'
            valign: 'middle'
            color: 0,0,0,1
            pos_hint: {"center_x": .5, "center_y": .6}
        Label:
            id: label2
            text: ""
            text_size: self.size
            halign: 'center'
            valign: 'middle'
            color: 0,0,0,1
            pos_hint: {"center_x": .5, "center_y": .4}
"""

class ScreenLogin(Screen):
    pass

class ScreenDisplayLabel(Screen):
    pass

class WindowManager(ScreenManager):
    pass

class Teste(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义全局属性存储需要传递的值
        self.value_to_display_1 = ""
        self.value_to_display_2 = ""

    def build(self):
        return Builder.load_string(KV)

    def login(self):
        user = self.root.get_screen('ScreenLogin').ids.user.text
        password = self.root.get_screen('ScreenLogin').ids.password.text
        
        # 登录验证逻辑
        if (user == '1' and password == '1') or (user == '2' and password == '2'):
            self.value_to_display_1 = "xxxxxxxxxxx"
            notification.notify(title='Login', message='Success!')
            # 调用方法获取第二个值
            self.Extract_Value_Login()
            # 更新显示页面的标签内容
            display_screen = self.root.get_screen('ScreenDisplayLabel')
            display_screen.ids.label1.text = self.value_to_display_1
            display_screen.ids.label2.text = self.value_to_display_2
            # 切换到显示页面
            self.root.current = "ScreenDisplayLabel"
            self.root.transition.direction = "left"
        else:
            # 登录失败的提示
            notification.notify(title='Login', message='Failed! Wrong user or password.')

    def Extract_Value_Login(self):
        # 将值赋值给全局属性
        self.value_to_display_2 = 'yyyyyyyyyyyyyyyy'

Teste().run()

关键修改说明

  1. 全局属性定义:在Teste类的__init__方法中定义self.value_to_display_1和self.value_to_display_2,替代原来的局部变量,确保跨方法和页面可访问
  2. 添加显示控件:在ScreenDisplayLabel中添加两个Label控件,设置id为label1和label2,用于展示文本
  3. 调整页面切换逻辑:将页面切换的代码移到登录验证成功的分支内,避免登录失败也切换页面
  4. 调用并赋值:登录成功后调用Extract_Value_Login方法获取第二个值,然后直接通过屏幕的ids给标签赋值

内容的提问来源于stack exchange,提问作者João Faria

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 02:15:42