Я пытаюсь создать переключатель в kivy, который при активации позволяет видеть пароль. Поэтому я хочу, чтобы код применял True к атрибуту пароля моего ввода пароля loginscreen, когда переключатель выключен, ввод текста пароля и наоборот. Если есть другой способ решить мою проблему, пожалуйста, сообщите мне. Я новичок в python и киви. Это мой код python: -
from kivy.app import App
from kivy.lang import Builder
from kivy.core.text import LabelBase
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.switch import Switch
from kivy.animation import Animation
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
import json, glob
from datetime import datetime
from pathlib import Path
import random
import re
Builder.load_file('design.kv')
class LoginScreen(Screen):
def sign_up(self):
self.manager.transition.direction = "left"
self.manager.current = "signup_screen"
def login(self, uname, pword):
with open("Emotional Quoter/users.json") as file:
users = json.load(file)
if uname in users and users[uname]['password'] == pword:
self.manager.current = "login_screen_success"
else:
self.ids.login_wrong.text = "Wrong username or password!"
def view_pass(self, instance, value):
if value is True:
self.ids.password = False
else:
self.ids.password = True
class SignupScreen(Screen):
def add_user(self, uname, pword):
with open("Emotional Quoter/users.json") as file:
users = json.load(file)
if len(pword) < 8:
self.ids.wrong_password.text = "Make sure your password is at lest 8 letters"
elif re.search('[0-9]', pword) is None:
self.ids.wrong_password.text = "Make sure your password has a number in it"
elif re.search('[A-Z]', pword) is None:
self.ids.wrong_password.text = "Make sure your password has a capital letter in it"
elif uname in users:
self.ids.wrong_password.text = "Username already exists"
else:
users[uname] = {'username': uname, 'password': pword,
'created': datetime.now().strftime("%Y-%m-%d %H-%M-%S")}
with open("Emotional Quoter/users.json", 'w') as file:
json.dump(users, file)
self.manager.current = "sign_up_screen_success"
class SignupScreenSuccess(Screen):
def success(self):
self.manager.transition.direction = 'right'
self.manager.current = "login_screen"
class LoginScreenSuccess(Screen):
def log_out(self):
self.manager.transition.direction = "down"
self.manager.current = "login_screen"
def get_quote(self, feel):
feel = feel.lower()
available_feelings = glob.glob("Emotional Quoter/quotes/*txt")
available_feelings = [Path(filename).stem for filename in
available_feelings]
if feel in available_feelings:
with open(f"Emotional Quoter/quotes/{feel}.txt", encoding="utf-8") as file:
quotes = file.readlines()
self.ids.quote.text = random.choice(quotes)
else:
self.ids.quote.text = "Try another feeling"
class RootWidget(ScreenManager):
pass
class MainApp(App):
def build(self):
return RootWidget()
if __name__ == "__main__":
MainApp().run()
, а это мой код kivy:
<LoginScreen>:
GridLayout:
cols: 1
padding: 15, 15
spacing: 30, 30
GridLayout:
cols: 1
Label:
font_size: "30sp"
text: "User Login"
bold: True
TextInput:
size_hint: (.2, None)
height: 50
multiline: False
id: username
hint_text: "Username"
RelativeLayout:
TextInput:
size_hint: (.9, None)
height: 50
multiline: False
id: password
hint_text: "Password"
pos_hint: {'center_x': 0.45, 'center_y': 0.4}
password:
Switch:
pos_hint: {'center_x': 0.96, 'center_y': 0.43}
active: False
on_active: root.view_pass(self, self.active)
RelativeLayout:
Button:
text: "Login"
on_press: root.login(root.ids.username.text, root.ids.password.text)
size_hint: 0.3, 0.5
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
Label:
id: login_wrong
text: ""
GridLayout:
cols: 2
size_hint: 0.2, 0.2
padding: 10, 10
spacing: 10, 10
Button:
text: "Forgot Password?"
background_color: 1, 1, 1, 0
opacity: 1 if self.state == 'normal' else 0.5
color: 0.1, 0.7, 1, 1
bold: True
Button:
text: "Signup here!!!"
on_press: root.sign_up()
background_color: 1, 1, 1, 0
opacity: 1 if self.state == 'normal' else 0.5
color: 0.1, 0.7, 1, 1
bold: True
<SignupScreen>:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'back.jpg'
GridLayout:
cols:1
Label:
font_size: 30
text: "Signup for a space adventure!!!"
bold: True
TextInput:
id:username
hint_text: "Username"
TextInput:
id:password
hint_text: "Password"
Label:
id: wrong_password
text: ""
Button:
text: "Signup"
on_press: root.add_user(root.ids.username.text , root.ids.password.text)
<SignupScreenSuccess>:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'back.jpg'
GridLayout:
cols: 1
Label:
text: "Signup Successful"
Button:
text: "Login Page"
on_press: root.success()
<LoginScreenSuccess>:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'back.jpg'
GridLayout:
cols: 1
Button:
text: "Logout"
on_press: root.log_out()
Label:
text: "How do you feel?"
TextInput:
id: feeling
hint_text: "Things to try: Happy, Sad, Unloved..."
Button:
text: "Enlighten me!"
on_press: root.get_quote(root.ids.feeling.text)
Label:
id: quote
text: ""
<RootWidget>:
LoginScreen:
name: "login_screen"
SignupScreen:
name: "signup_screen"
SignupScreenSuccess:
name: "sign_up_screen_success"
LoginScreenSuccess:
name: "login_screen_success"
Пожалуйста, помогите мне.