如何在 Kivy 中跨多个屏幕更新 Id 值

问题描述

我目前正在尝试使用 Kivy 创建一个密码管理器应用程序,为您生成和存储密码。我在尝试保存已生成的当前密码时遇到了一个问题,因为当它进入下一个屏幕时,它求助于使用第一个 id 值而不是更新的值。我尝试使用屏幕管理器功能获取屏幕来检索该值。

这是什么原因以及如何解决

main.py:

class passwordCreator(Screen):

    def updatePassword(self): 
        current = self.ids.currentPassword
        current.text = self.generatePassword()


    def generatePassword(self):
        lower = string.ascii_lowercase
        upper = string.ascii_uppercase
        num = string.digits
        symbols = string.punctuation
        All = lower + upper + num + symbols 
        # Store all possible strings in one large string 

        temp = random.sample(All,random.randint(8,16))
        password = "".join(temp)

        return password
    
    def getpassword(self): 
        current = self.ids.currentPassword
        return current.text

    def save(self):
        password = self.getpassword()
        saves.append(password)
        print(saves)



class savingScreen(Screen):
    pass
    
class WindowManager(ScreenManager):
    pass


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

class MyApp(App):

    def build(self):
        return kv


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

我的.kv:


WindowManager:
    Menu:
    passwordCreator:
    savingScreen: 


<Menu>
    name: "menu"

    GridLayout:
        cols: 1 
        size: root.width,root.height
        
        Button:
            text:"Create Password"
            on_release: 
                app.root.current = "creatingPassword"

        Button:
            text: "Password List"


<passwordCreator>
    name: "creatingPassword"

    GridLayout:
        size: root.size
        rows: 3
        
        Label:
            id: currentPassword
            text: root.getpassword()

        Button:
            text: "Generate!"
            on_release:
                root.updatePassword()

        GridLayout: 
            cols: 3

            Button: 
                text: "Save"
                on_release:
                    app.root.current = "saving" 
            

            Button: 
                text: "Back" 
                on_release: 
                    app.root.current = "menu"

<savingScreen> 
    name: "saving" 

    GridLayout:
        size:root.size
        rows: 2 

        GridLayout:
            size:root.size 
            cols: 2

            Label: 
                text: root.manager.get_screen("creatingPassword").ids.currentPassword.text
            
            TextInput: 
                text: "What application is password for..."
                multiline: False
        
        Button:
            text: "SAVE"

感谢您的帮助!

解决方法

使用以下方法设置 Label 的文本:

text: root.manager.get_screen("creatingPassword").ids.currentPassword.text

不能工作只是因为 kivy 不能为那个表达式做自动绑定。解决这个问题的一种方法是在您的 savingScreen 中定义一个属性,并编写您自己的代码来更新它:

class savingScreen(Screen):
    current_password = StringProperty('')

    def on_enter(self,*args):
        # update the curent_password property
        self.current_password = self.manager.get_screen('creatingPassword').ids.currentPassword.text

然后您可以在 kv 中引用该属性:

        Label: 
            # text: root.manager.get_screen("creatingPassword").ids.currentPassword.text
            text: root.current_password