I create a simple app with Python and KivyMD. When the app running, the login screen asks for input. It was something like this:
from kivy.app import App from kivymd.app import MDApp from kivy.lang import Builder from kivy.core.window import Window from kivy.properties import StringProperty from kivy.uix.screenmanager import ScreenManager,Screen class Login(Screen): def __init__(self, *args, **kwargs): super(login,self).__init__(*args,**kwargs) self.user="" self.username=self.ids.usernamebox.text self.password=self.ids.passwordbox.text def checkusername(self) if self.username=="something" and self.password=="something": self.user="admin" else: self.user="member" sm.current='nextscreen' class NextScreen(Screen): def somefeatures(self): if Login.user="admin": print(Login.user) pass if Login.user="member": print(Login.user) pass class MyApp(MDApp): def build(self): global sm sm=ScreenManager() sm.add_widget(Logon(name='login')) sm.add_widget(NextScreen(name='nextscreen'))
also, the kivy code if it any helps:
<Login> MDBoxLayout: orientation: "vertical" MDTextField: id: usernamebox hint_text: "Username" MDTextField: id: passwordbox hint_text: "Password" MDFillRoundFlatButton: id: login text: "Login" on_release: root.checkusername() pos_hint: {"center_x":.5} <NextScreen> MDBoxLayout: orientation: "vertical" MDFlatButton: text: "test" on_press: root.somefeatures()
I'm using kivy screen manager to switch between the classes.
What I try to achieve is how to make somefeatures
prints the correct value of user after the user logged in
So far I've tried:
- initializing the login class
l=login()
and access withl.user
, didn't work because__init__
returns 'user' as empty string - create
global user
, but still empty string - create
setuser
andgetuser
method within class login, but still can't return the value - initializing user outside the class
user=""
before any other classes, also empty string - Accessing the value of login class like the code above, it says
type object Login has no attribute user
Is there is a way to achieve this? I want the value of user is still remembered as long as the app running (or until I make logout button, idk)
Thank you so much.
https://stackoverflow.com/questions/66932264/cant-access-a-modified-attribute-from-another-screen-class April 03, 2021 at 10:26PM
没有评论:
发表评论