【问题标题】:I am getting an attribute error ( AttributeError: 'super' object has no attribute '__getattr__') while running my script运行脚本时出现属性错误(AttributeError: 'super' object has no attribute '__getattr__')
【发布时间】:2021-07-28 20:34:18
【问题描述】:

我不知道为什么会收到此属性错误。我的代码在从用户和密码输入框中获取值时发现错误:

错误 - 文件“d:\Desktop\kivymd\login.py”,第 36 行,验证中 用户 = self.root.ids.user.text 文件“kivy\properties.pyx”,第 864 行,在 kivy.properties.ObservableDict.__getattr__ AttributeError: 'super' 对象没有属性 '__getattr__'

from kivy.config import Config
from kivy import Config
Config.set('graphics', 'width', '400')
Config.set('graphics', 'height', '500')
Config.set('graphics', 'resizable', 'False')
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
import sqlite3


class MainWindow(Screen):
    pass


class SecondWindow(Screen):
    pass


class WindowManager(ScreenManager):
    pass

class MainApp(MDApp):
   
    def build(self):
      
       Config.set('graphics', 'resizable', False)
       self.theme_cls.theme_style = "Dark"
       self.theme_cls.primary_palette = "Purple"
       return Builder.load_file("login.kv")            
    def verify(self):            
        conn = sqlite3.connect("master.db")
        cur = conn.cursor()
        
        
        user = self.root.ids.user.text
        password = self.root.ids.password.text
        print(user)
        print(password)
        if user=="" or password=="" :  
           self.root.ids.error.markup=True
           self.root.ids.error.text = "[b][color=#f50539]materuser & masterpassword required ![/b][/color]" 
           
        else:           
            cur.execute("SELECT rowid , *FROM master_database WHERE master_users = ?", (user,))
            
            c=cur.fetchone()
            conn.commit()
            conn.close()
        
            if c==None:
                self.root.ids.error.markup=True
                
                self.root.ids.error.text = f"No data for [color=#f50539][b]{user}[/b][/color]\nKindly register for [color=#f50539][b]new user ![/b][/color]" 
               
                                                    
            else:
                    
                    if c[2] == password:
                        self.root.ids.error.markup=True
                        self.root.ids.error.text = "[b]Successfully verified.Click to Login[b]"
                       # self.root.ids.log_in.on_release.app.root.current = "second"
                        
                        
                    else:
                        self.root.ids.error.markup=True
                        self.root.ids.error.text = "[b]Incorrect Password ![b]"   
MainApp().run() 



  

我的 .kv 文件 ----->

WindowManager:
    MainWindow:
    SecondWindow:

<MainWindow>:
    name: "main"

    MDCard:
        size_hint:None,None
        size:400,500
        pos_hint:{"center_x":.5,"center_y":.5}
        elevation:10
        padding:25
        spacing:25

        orientation:"vertical"

        MDLabel:
            markup:True
            id:welcome_label
            text:"[b]PASSWORD MANAGER[/b]"
            font_size:40
            halign: "center"
            size_hint_y:  None
            height:self.texture_size[1]
            padding_y:-10  

        MDTextFieldRound:
            id:user
            hint_text: "master usename"
            icon_right: "account"
            size_hint_x: None
            width:200
            font_size:18
            pos_hint:{"center_x":0.5}
        MDTextFieldRound:
            id:password
            hint_text: "master password"
            icon_right: "eye_off"
            size_hint_x: None
            width:200
            font_size:18
            pos_hint:{"center_x":0.5}
            password:True

        MDRoundFlatButton:
            id : verify
            text:"VERIFY"   
            font_size:12
            pos_hint:{"center_x":.5}
            on_press: app.verify()
                
<SecondWindow>:
    name: "second"
    Button:
        text: "Go Back"
        on_release:
            app.root.current = "main"
            root.manager.transition.direction = "right"

【问题讨论】:

    标签: python kivy kivymd


    【解决方案1】:

    您正试图从没有ids 的对象访问idsuserpassword ids 将位于 MainWindowids 中。因此,每当您有代码尝试访问这些 ids 时,例如:

        user = self.root.ids.user.text
        password = self.root.ids.password.text
    

    您应该修改该代码以访问MainWindowids

        user = self.root.get_screen('main').ids.user.text
        password = self.root.get_screen('main').ids.password.text
    

    以上代码调用ScreenManager(即self.root)的get_screen()方法来访问MainWindow。然后你就可以访问那个对象的ids了。

    请注意,kv 中定义的ids 仅出现在作为当前规则根的对象中。而你所有的ids 都在&lt;MainWindow&gt; 规则中定义。

    【讨论】:

      猜你喜欢
      • 2023-01-22
      • 1970-01-01
      • 2023-03-23
      • 2022-10-13
      • 2023-01-12
      • 2014-06-01
      • 1970-01-01
      • 2023-02-18
      • 1970-01-01
      相关资源
      最近更新 更多