【问题标题】:Can't access a modified attribute from another screen class无法从另一个屏幕类访问修改后的属性
【发布时间】:2021-04-03 14:26:58
【问题描述】:

我用 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__ 将“用户”返回为空字符串,所以不起作用
  2. 创建global user,但仍为空字符串
  3. 在类登录中创建setusergetuser方法,但仍然无法返回值
  4. 在任何其他类之前初始化类user=""之外的用户,也是空字符串
  5. 像上面的代码一样访问登录类的值,上面写着type object Login has no attribute user

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

非常感谢。

【问题讨论】:

  • 你能附上一个 minimal 工作示例吗?
  • 对不起,我不明白你的意思?
  • 请附上一段代码,无需任何操作即可运行,也无需不必要的函数和行
  • 我已经编辑了代码。

标签: python kivy kivymd


【解决方案1】:

这是 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没有user属性。
猜你喜欢
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多