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

Kivy/KivyMD认证页面:认证通过后从Python代码加载第二个屏幕的实现方法求助

解决认证通过后从Python代码切换到第二个屏幕的问题

我来帮你搞定这个认证后跳转屏幕的需求!你的核心目标是先在Python代码里完成账号密码的校验逻辑,确认通过后再切换到第二个屏幕,咱们一步步修改现有代码:

先梳理现有代码的问题

  • 登录按钮的on_press直接写了screen2,没有触发Python里的认证方法logger(),跳过了校验逻辑
  • logger()方法里认证通过后只是创建了一个Screen对象,但没有把它加入到ScreenManager,也没执行切换屏幕的操作

修改后的完整代码

app.py 代码调整

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import ObjectProperty

class my_layout(FloatLayout):
    screen_mngr = ObjectProperty(None)

class myapp(MDApp):
    def build(self):
        self.theme_cls.theme_style = "Light"
        self.theme_cls.primary_palette = "BlueGray"
        return Builder.load_file("app.kv")

    def logger(self):
        # 获取输入框里的账号和密码
        username = self.root.ids.user.text
        password = self.root.ids.password.text
        
        if username == 'admin' and password == 'admin':
            # 认证通过:更新欢迎文本,然后切换到第二个屏幕
            self.root.ids.welcome_label.text = f'Sup {username}!'
            # 直接操作ScreenManager切换到已定义的screen2
            self.root.ids.screen_mngr.current = 'screen2'
            # 可选优化:清空输入框,避免下次登录残留内容
            self.root.ids.user.text = ""
            self.root.ids.password.text = ""
        else:
            # 认证失败:提示错误信息
            self.root.ids.welcome_label.text = 'Wrong credentials'

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

app.kv 代码调整

my_layout:
    screen_mngr: screen_mngr

ScreenManager:
    id: screen_mngr
    home: home

    Screen:
        id: home
        name: 'home'
        MDCard:
            size_hint: None, None
            size: 450, 600
            pos_hint: {"center_x": 0.5, "center_y": 0.5}
            elevation: 10
            padding: 25
            spacing: 25
            orientation: 'vertical'

            MDLabel:
                id: welcome_label
                text: "WELCOME"
                font_size: 40
                halign: 'center'
                size_hint_y: None
                height: self.texture_size[1]
                padding_y: 15

            MDTextField:
                id: user
                hint_text: "username"
                icon_right: "account"
                size_hint_x: None
                width: 200
                font_size: 18
                pos_hint: {"center_x": 0.5}

            MDTextField:
                id: password
                hint_text: "password"
                icon_right: "eye-off"
                size_hint_x: None
                width: 200
                font_size: 18
                pos_hint: {"center_x": 0.5}

            MDRoundFlatButton:
                text: "LOG IN"
                font_size: 12
                pos_hint: {"center_x": 0.5}
                # 修改这里:点击按钮先触发Python的认证方法
                on_press: app.logger()

    Screen:
        id: screen2
        name: 'screen2'
        MDRoundFlatButton:
            text: "This is Second Screen\nGo to Screen1"
            size_hint: 0.2,0.1
            pos_hint: {"center_x":0.5,"y":0.5}
            on_press: screen_mngr.current = "home"

关键修改点说明

  1. 登录按钮逻辑:把原来直接跳转的on_press: screen2改成on_press: app.logger(),确保点击后先跑Python里的认证校验
  2. 认证后的跳转:因为你已经在kv里定义了screen2,不需要重新创建Screen对象,直接通过self.root.ids.screen_mngr.current = 'screen2'就能切换屏幕
  3. 用户体验优化:认证通过后清空输入框,避免下次登录时残留旧内容

这样调整后,用户点击登录按钮会先完成账号密码校验,只有通过后才会跳转到第二个屏幕,完全符合你的需求~

内容的提问来源于stack exchange,提问作者aze2201

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 17:37:42