我尝试导入 kivy 缓存对象,附加密钥和用户名对象并在 LoginSuccess 类中检索它,但是当我在 LoginScreen 类中附加时似乎没有附加对象。当它没有附加在一个类中时,它可以正常工作。我该如何解决这个问题?
"""Logic of the program resides. """
from kivy.uix.label import Label
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.cache import Cache
from datetime import datetime
import json
import hashlib
import glob
from pathlib import Path
import random
# Build the kivy design file
Builder.load_file('design.kv')
# register a new Cache
Cache.register('design')
# create an object + id
# key = 'objectid'
# instance = Label(text=text)
# Cache.append('mycache', key, instance)
# retrieve the cached object
# instance = Cache.get('mycache', key)
# Every rule in the kivy file has to be represented as a class in the main python file
class LoginScreen(Screen):
def sign_up(self):
# access the sign up screen by the name
self.manager.current = "sign_up_screen"
print("sign up button pressed...")
def login(self,username,password):
# open up json file
key = 'username'
use = Label(text='jaja')
Cache.append('design', key, use)
with open('users.json') as file:
users = json.load(file)
# check for username present, if not present show login failed
if username in users:
global key_username
key_username = username
p_s= str.encode(password)
# hash password
p_s_hash = hashlib.sha256(p_s)
hex_dig = p_s_hash.hexdigest()
if hex_dig == users[username]["password"]:
# create an object + id
key = 'username'
use = Label(text=username)
Cache.append('design', key, use)
self.manager.current = "login_success"
else:
self.ids.wrong_login.text = "Incorrect username or password please try again"
else:
self.ids.wrong_login.text = "Incorrect username or password please try again"
# compare decrypted password with password passed in if matches switch to login success page
# create an object + id
key = 'username'
use = Label(text=username)
Cache.append('design', key, use)
# else show login failed
class LoginSuccessScreen(Screen):
def __init__(self,**kwargs):
super(Screen, self).__init__(**kwargs)
# retrieve from cache
username = Cache.get('design', 'username',default="username")
print(username)
# if username is None:
# print("No username present")
# else:
# welcome_text = Label(pos=(0,250), text="Welcome "+username+ "!")
# self.add_widget(welcome_text)
def log_out(self):
self.manager.transition.direction = "right"
self.manager.current = "login_screen"
def get_quotes(self, feel):
feel = feel.lower()
available_feelings = glob.glob("quotes/*.txt")
# acquire available feelings from quote directory
print(available_feelings)
available_feelings = [Path(filename).stem for filename in available_feelings]
# check if feeling is in available_feelings
if feel in available_feelings:
with open(f"quotes/{feel}.txt") as file:
quotes = file.readlines()
quote = random.choice(quotes)
self.ids.feeling_result.text = quote
else:
self.ids.feeling_result.text = "Not found :("
class RootWidget(ScreenManager):
pass
class SignUpScreen(Screen):
# add username
def add_user(self, username, password):
print("username",username,"password",password)
with open('users.json') as file:
users = json.load(file)
password_bytes = hashlib.sha256(str.encode(password))
hex_dig = password_bytes.hexdigest()
users[username] = {"username": username, "password": hex_dig, "created": datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
print(users)
#overwrite json file
with open('users.json','w') as file:
json.dump(users,file)
# switch to success screen
self.manager.current = "sign_up_success"
class SignUpSuccessScreen(Screen):
def login(self):
self.manager.current = "login_screen"
class MainApp(App):
def build(self):
return RootWidget()
if __name__ =="__main__":
MainApp().run()
以下是我的 kivy 设计文件:
<LoginScreen>:
GridLayout:
cols: 1
GridLayout:
cols: 1
Label:
text: "User Login"
TextInput:
id: username
hint_text: "Username"
TextInput:
id: password
hint_text: "Password"
password: True
Button:
text: "Login"
on_press: root.login(root.ids.username.text,root.ids.password.text)
Label:
id: wrong_login
text: ""
GridLayout:
cols: 2
Button:
text: "Sign Up"
on_press: root.sign_up()
Button:
text: "Forgot Password?"
<SignUpScreen>:
GridLayout:
cols: 1
Label:
text: "Sign up for a free account"
TextInput:
id: username
hint_text: "Username"
TextInput:
id: password
hint_text: "Password"
Button:
text: "Submit"
on_press: root.add_user(root.ids.username.text, root.ids.password.text)
<SignUpSuccessScreen>:
GridLayout:
cols: 1
Label:
text: "Sign Up Successful!"
Button:
text: "Login"
on_press: root.login()
<LoginSuccessScreen>:
GridLayout:
cols: 1
Label:
id: welcome
text: ""
Button:
text: "Logout"
on_press: root.log_out()
Label:
text: "How are you feeling?"
TextInput:
id: feeling
hint_text: "Things to try: sad, unloved, happy"
Button:
text: "Enlighten me"
on_press: root.get_quotes(root.ids.feeling.text)
Label:
id: feeling_result
text: ""
multiline: True
<RootWidget>:
LoginScreen:
name: "login_screen"
SignUpScreen:
name: "sign_up_screen"
SignUpSuccessScreen:
name: "sign_up_success"
LoginSuccessScreen:
name: "login_success"