【发布时间】: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"
感谢您的帮助!
【问题讨论】: