简体   繁体   English

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

[英]how to update Id values across multiple screens in Kivy

I am currently attempting to create a password manager app using Kivy that generates and stores passwords for you.我目前正在尝试使用 Kivy 创建一个密码管理器应用程序,为您生成和存储密码。 I came across an issue when attempting to save the current password that has been generated as when it goes to the next screen it resorts to using the very first id value not the updated value.我在尝试保存已生成的当前密码时遇到了一个问题,因为当它进入下一个屏幕时,它求助于使用第一个 id 值而不是更新的值。 I try to retrieve the value using the screen manager function get screen.我尝试使用屏幕管理器功能获取屏幕来检索值。

Any reason why this is and how to solve?这是什么原因以及如何解决?

main.py:主要.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()

my.kv:我的.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"

Thanks for any help !谢谢你的帮助 !

Setting the text of the Label using:使用以下方法设置Label的文本:

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

won't work only because kivy can't do the automatic binding for that expression.不会工作只是因为 kivy 无法为该表达式进行自动绑定。 A way to get around that is to define a property in your savingScreen , and write your own code to update it:解决这个问题的一种方法是在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

Then you can reference that property in the kv :然后您可以在kv引用该属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM