2021年4月5日星期一

Can't access a modified attribute from another screen class

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:

  1. initializing the login class l=login() and access with l.user, didn't work because __init__ returns 'user' as empty string
  2. create global user, but still empty string
  3. create setuser and getuser method within class login, but still can't return the value
  4. initializing user outside the class user="" before any other classes, also empty string
  5. 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

没有评论:

发表评论