【问题标题】:kivymd: AttributeError: 'super' object has no attribute '__getattr__'kivymd:AttributeError:“超级”对象没有属性“__getattr__”
【发布时间】:2021-07-12 00:23:24
【问题描述】:

这个 .kv 文件我通过Builder.load_file('style.kv') lode Kivy 文件

 ScreenManager:
      Sign_in:
      Sign_up:

<Sign_in>:
    name: 'sign in'
    MDScreen:
       id: sign_in
       md_bg_color: [35/255, 59/255, 54/255, 1]
       MDCard:
          size_hint: None, None
          size: 320, 400
          pos_hint: {"center_x":.5, "center_y":.5}
          elevation: 15
          md_bg_color: [35/255, 49/255, 48/255, 1]
          padding: 20
          spacing: 30
          orientation: "vertical"
          MDLabel:
              text: "LOGIN"
              font_style: 'Button'
              font_size: 45
              halign: "center"
              size_hint_y: None
              height: self.texture_size[1]
              padding_y: 15
          MDTextFieldRound:
              id: username
              hint_text: "username"
              icon_right: "account"
              size_hint_x: None
              width: 220
              pos_hint: {"center_x":.5}
              color_active: [1,1,1,1]
          MDTextFieldRound:
              id: Pass
              hint_text: "Password"
              icon_right: "eye-off"
              size_hint_x: None
              width: 220
              pos_hint: {"center_x":.5}
              password: True
              color_active: [1,1,1,1]
          MDTextFieldRound:
              id: Email
              hint_text: "Email Id"
              icon_right: "email"
              size_hint_x: None
              width: 220
              pos_hint: {"center_x":.5}
              password: True
              color_active: [1,1,1,1]
          MDRoundFlatButton:
              text: "Sign In"
              pos_hint: {"center_x":.5}
              font_size: 15
              on_press: app.show_data()
          Widget:
              size_hint_y: None
              height: 30
<Sign_up>:
    name: 'sign up'

我尝试了很多东西,但没有解决这个错误.py文件如何解决?

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager

class Sign_in(Screen):
    pass


class Sign_up(Screen):
    pass

se = ScreenManager()
se.add_widget(Sign_in(name='sign in'))
se.add_widget(Sign_up(name='sign up'))


class Chatting(MDApp):
    def build(self):
        return Builder.load_file('style.kv')

    def show_data(self):
        print(self.root.ids.username.text)


if __name__ == '__main__':
    Chatting().run()

【问题讨论】:

    标签: python kivy mobile-application kivymd


    【解决方案1】:

    代码行:

    print(self.root.ids.username.text)
    

    正在尝试通过您的App 的根小部件访问usernameid,但根小部件是ScreenManager,其中不包含id。根据您的kvusername idSign_in 小部件中定义。

    您可以使用ScreenManagerget_screen() 方法访问username Screen 小部件,如下所示:

    print(self.root.get_screen('sign in').ids.username.text)
    

    注意以下几行:

    se = ScreenManager()
    se.add_widget(Sign_in(name='sign in'))
    se.add_widget(Sign_up(name='sign up'))
    

    正在创建另一个未在您的App 中实际使用的小部件树。使用的小部件树由以下行构建:

    return Builder.load_file('style.kv')
    

    所以上面那三行可以去掉。

    【讨论】:

    • 感谢您帮助我清除了我的错误
    【解决方案2】:

    我想为任何未来的研究人员添加一件事是添加自我。到您传递的任何变量。

    例如,如果您想将小部件中的变量传递给应用程序,则必须将应用程序中的变量声明为 self.some_variable = self.self.root.get_screen('sign in').ids.username 。文本。然后您可以使用此变量来执行功能。

    在上面的代码中,您可能希望将 username.text 与您存储的用户名进行比较。如果你在你的应用中设置变量 some_variable = self.root.get_screen('sign in').ids.username.text,它会给你同样的错误。

    我花了好几天才弄明白,所以我希望有一天它对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-11-17
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      相关资源
      最近更新 更多