我的两个屏幕重叠,但前提是弹出窗口已被激活

My two screens overlap but only if the popup has been activated

对于我的第一个 Kivy 项目,我想创建一个基本的应用程序来存储我的密码。如果您碰巧输入了错误的访问代码,将出现一个弹出窗口,显示“密码不正确”。每当我正确输入密码时,首先尝试转换工作没有问题,但如果密码错误并出现弹出窗口,任何后续尝试都会导致两个页面重叠。

main.py:

import kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout

class PassWindow(Screen):
    pass

class MainWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

class PassPopup(FloatLayout):
    pass

def openPopup():
    show = PassPopup()

    global popupWin
    popupWin = Popup(title="Error", content=show, size_hint=(None, None), size=(400, 400))

    popupWin.open()

def closePopup():
    popupWin.dismiss()



kv = Builder.load_file("my.kv")

class MyApp(App):
    def build(self):
        return kv

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

my.kv:

#:import openPopup main.openPopup
#:import closePopup main.closePopup

WindowManager:
    PassWindow:
    MainWindow:

<Button>:
    font_size: 30
    color: 0.33, 0, 0.51, 1

<Label>:
    color: 1, 1, 1, 1

<PassWindow>:
    name: "passWin"
    id: passWin
    Label:
        text: "Password Storage"
        pos_hint: {"top": 1}
        font_size: 50
        size_hint: 1, 0.2

    Label:
        text: "Please Input Code to Access"
        pos_hint: {"top": .8}
        font_size: 50
        size_hint: 1, 0.2

    TextInput:
        id: codeInput
        multiline: False
        password: True
        font_size: 55
        size_hint: 0.6, 0.2
        pos_hint: {"x": .2, "top": .6}

    Button:
        text: "Submit"
        size_hint: 0.5, 0.2
        pos_hint: {"x": .25, "top": .35}
        
        on_release:
            app.root.current = "mainWin" if codeInput.text == "1234" else openPopup()
            app.root.transition.direction = "left"
            codeInput.text = ""

<PassPopup>:
    Label:
        text: "Incorrect Password"
        size_hint: 0.6, 0.2
        pos_hint: {"x": 0.2, "top": 0.8}
        font_size: 30

    Button:
        text: "Close"
        size_hint: 0.8, 0.2
        pos_hint: {"x": 0.1, "top": 0.3}

        on_release:
            closePopup()


<MainWindow>:
    name: "mainWin"

    Button:
        text: "Exit"
        size_hint: 1, 0.2

        on_release: 
            app.root.current = "passWin"
            root.manager.transition.direction = "right"

on_release: Submit Button 中的逻辑不正确。该行:

app.root.current = "mainWin" if codeInput.text == "1234" else openPopup()

如果提供的密码为“1234”,则将当前屏幕设置为“mainWin”,否则,将当前屏幕设置为openPopup()的return值(即None).我怀疑这是奇怪行为的原因。要解决此问题,请对 on_release 使用以下内容:

    on_release:
        if codeInput.text == "1234": app.root.current = "mainWin"
        else: openPopup()
        app.root.transition.direction = "left"
        codeInput.text = ""

参见documentation