微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。