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

如何实现Kivy登录界面输入指定密码后跳转至新界面

Solution: Switch to New Screen After Entering Correct Code

Got it, let's adjust your Kivy code to make the login interface disappear and show a new screen when you enter "12345" and press Enter. We'll use Kivy's ScreenManager—the standard tool for handling multiple screens in Kivy apps—to make this clean and maintainable.

Here's the full modified code with explanations:

from kivy.app import runTouchApp
from kivy.factory import Factory
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

# Keep your existing button and keypad classes with minor tweaks
class KeypadButton(Factory.Button):
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            Factory.FocusBehavior.ignored_touch.append(touch)
        return super(KeypadButton, self).on_touch_down(touch)

class Keypad(Factory.GridLayout):
    target = Factory.ObjectProperty(None, allownone=True)
    screen_manager = Factory.ObjectProperty(None)  # Add reference to ScreenManager

    def __init__(self, **kwargs):
        super(Keypad, self).__init__(**kwargs)
        self.cols = 3
        for x in list(range(1, 10)) + ['<-', 0, 'Enter']:
            btn = KeypadButton(text=str(x), on_release=self._on_release)
            self.add_widget(btn)

    def _on_focus(self, ti, value):
        self.target = value and ti

    def _on_release(self, instance, *largs):
        if self.target:
            if instance.text == 'Enter':
                # Check if input matches the target code
                if self.target.text == '12345':
                    # Switch to the new screen
                    self.screen_manager.current = 'new_screen'
                    self.target.text = ''  # Reset input for future use
                else:
                    print(f"Invalid code: {self.target.text}")
                    self.target.text = ''
            elif instance.text == '<-':
                if self.target.text:
                    self.target.text = self.target.text[:-1]
            else:
                self.target.text += str(instance.text)

# Define our screens
class LoginScreen(Screen):
    pass

class NewScreen(Screen):
    pass

runTouchApp(Builder.load_string('''
<KeypadTextInput@TextInput>:
    keypad: None
    on_focus: root.keypad._on_focus(*args)

<LoginScreen>:
    BoxLayout:
        orientation: 'vertical'
        KeypadTextInput:
            keypad: keypad
        Keypad:
            id: keypad
            screen_manager: root.manager  # Pass ScreenManager reference
            size_hint_x: 0.5
            pos_hint: {'center_x': 0.5}

<NewScreen>:
    Label:
        text: "Welcome! You've successfully logged in."
        font_size: 24
        color: 0, 0.6, 0.2, 1  # Custom green color for example

ScreenManager:
    LoginScreen:
        name: 'login_screen'
    NewScreen:
        name: 'new_screen'
'''))

Key Changes Explained:

  • ScreenManager Integration: We added ScreenManager to handle switching between the login interface (LoginScreen) and the new welcome screen (NewScreen). This is the standard, scalable way to manage multiple views in Kivy.
  • Screen Reference in Keypad: Added a screen_manager property to the Keypad class so it can access the ScreenManager and trigger screen switches when the correct code is entered.
  • Code Validation Logic: Modified the _on_release method to check if the input text is exactly "12345" when Enter is pressed. If it matches, we switch to the new screen; if not, we clear the input and print an invalid code message for feedback.
  • Customizable New Screen: The NewScreen currently has a simple welcome label—you can replace this with any widgets (images, buttons, layouts) from your schematic to match your desired interface.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 13:22:50