无法从另一个屏幕类访问修改后的属性

时间:2021-04-03 14:26:58

标签: python kivy kivymd

我使用 Python 和 KivyMD 创建了一个简单的应用程序。当应用程序运行时,登录屏幕会要求输入。事情是这样的:

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'))

此外,如果有帮助的话,kivy 代码:

<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()

我正在使用 kivy 屏幕管理器在课程之间切换。

我试图实现的是如何让somefeatures在用户登录后打印正确的用户值

到目前为止我已经尝试过:

  1. 初始化登录类 l=login() 并使用 l.user 访问,但无效,因为 __init__ 将“user”作为空字符串返回
  2. 创建global user,但仍然是空字符串
  3. 在类 login 中创建 setusergetuser 方法,但仍然无法返回值
  4. 在任何其他类之前在类 user="" 之外初始化用户,也是空字符串
  5. 像上面的代码一样访问登录类的值,它说type object Login has no attribute user

有没有办法实现这一目标?我希望只要应用程序运行(或直到我制作注销按钮,idk),用户的价值仍然被记住

非常感谢。

1 个答案:

答案 0 :(得分:0)

这是 main.py

from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import StringProperty, ObjectProperty, NumericProperty, ReferenceListProperty
Builder.load_file('the.kv')

class fscreen(Widget):

    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        theapp.user = ''

    def checkusername(self):
            
        self.username = self.ids.password.text
        self.password = self.ids.username.text
        if self.username == "something" and self.password == "something":
            theapp.user = "admin"
            
        else:
            theapp.user = "member"


        print(self.username, self.password, theapp.user)
        theapp.screenm.current = "second screen"

        theapp.secscreen.userlabel = 'welcome   ' + theapp.user 

class secscreen(Widget):
    userlabel = StringProperty()
    def __init__(self,**kwargs):
        super().__init__(**kwargs)

        self.userlabel = '' 
   

class thscreen(Widget):
    def __init__(self, **kwargs):
        supper().__init__(**kwargs)
        pass

class theapp(App):
    def build(self):
        self.screenm = ScreenManager()

        self.fscreen = fscreen()
        screen = Screen(name = "first screen")
        screen.add_widget(self.fscreen)
        self.screenm.add_widget(screen)

        self.secscreen = secscreen()
        screen  = Screen(name = "second screen")
        screen.add_widget(self.secscreen)
        self.screenm.add_widget(screen)

        return self.screenm

if __name__ == "__main__":
    theapp = theapp()
    theapp.run()

.kv 文件

<fscreen>

    TextInput:
        size: root.width*0.2, root.height*0.1
        pos: root.width*0.45, root.height*0.5
        id: username
        hint_text: 'username'

    TextInput:
        size: root.width*0.2, root.height*0.1
        pos: root.width*0.45, root.height*0.4
        id: password
        hint_text: 'password'

    Button:
        size: root.width*0.2, root.height*0.1
        pos: root.width*0.45, root.height*0.1
        text: 'login or something'
        on_press: root.checkusername()

<secscreen>
    Label:
        text: root.userlabel
        size: root.width*0.2, root.height*0.1
        pos: root.width*0.45, root.height*0.4

你的问题与类有关,你应该阅读更多关于 python 类的信息。

fscreen

secscreen