【问题标题】:how to update Id values across multiple screens in Kivy如何在 Kivy 中跨多个屏幕更新 Id 值
【发布时间】:2021-07-21 12:19:48
【问题描述】:

我目前正在尝试使用 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"

感谢您的帮助!

【问题讨论】:

    标签: python kivy updating


    【解决方案1】:

    设置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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多