【问题标题】:Kivy AttributeError: 'NoneType' object has no attribute 'get_screen'Kivy AttributeError:“NoneType”对象没有属性“get_screen”
【发布时间】:2020-02-16 20:31:39
【问题描述】:

我正在尝试使用 Buttons 的数量初始化 Screen,具体取决于对我的数据库的查询结果,但我得到的是 AttributeError。我认为这是因为当我初始化我的应用程序时没有 Information Screen 并阅读此答案:AttributeError: 'NoneType' object has no attribute 'current' 我需要使用Clock 来延迟我的Finder 类的初始化,以便我的Information 类有时间创建,但我不太确定如何在__init__ 方法中执行此操作,或者如果这甚至是正确的方法?

class Finder(Screen):
    layout_profiles = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Finder, self).__init__(**kwargs)
        self.mydb = mysql.connector.connect(host="localhost", user="root", passwd="", database="")
        self.cur = self.mydb.cursor(buffered=True)
        self.cur.execute("SELECT gender, age, name FROM users WHERE location = %s AND date_of_visit = %s",
                         (self.manager.get_screen('information').location.text,
                          self.manager.get_screen('information').date))
        self.mydb.commit()
        self.results = cur.fetchall()
        self.cur.close()
        self.mydb.close()
        if self.results:
            for result in self.results:
                if result[0] == "male":
                    male_btn = MaleButton(text="{}, {}".format(result[1], result[2]))
                    self.layout_profiles.add_widget(male_btn)
                elif result[0] == "female":
                    female_btn = FemaleButton(text="{}, {}".format(result[1], result[2]))
                    self.layout_profiles.add_widget(female_btn)
                else:
                    unknown_btn = NoGenderButton(text="{}, {}".format(result[1], result[2]))
                    self.layout_profiles.add_widget(unknown_btn)

错误

line 249, in __init__
     (self.manager.get_screen('information').location.text,
 AttributeError: 'NoneType' object has no attribute 'get_screen'

【问题讨论】:

  • 属性self.managerNone。你确定你正确地初始化了你的对象吗?

标签: python kivy


【解决方案1】:

, dt这是一种在屏幕初始化之后执行所有东西的方法

class Finder(Screen):
    layout_profiles = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Finder, self).__init__(**kwargs)
        self.mydb = mysql.connector.connect(host="localhost", user="root", passwd="", database="")
        self.cur = self.mydb.cursor(buffered=True)
        Clock.schedule_once(self.do_stuff)

    def do_stuff(self, dt):
        self.cur.execute("SELECT gender, age, name FROM users WHERE location = %s AND date_of_visit = %s",
                         (self.manager.get_screen('information').location.text,
                          self.manager.get_screen('information').date))
        self.mydb.commit()
        self.results = self.cur.fetchall()
        self.cur.close()
        self.mydb.close()
        if self.results:
            for result in self.results:
                if result[0] == "male":
                    male_btn = MaleButton(text="{}, {}".format(result[1], result[2]))
                    self.layout_profiles.add_widget(male_btn)
                elif result[0] == "female":
                    female_btn = FemaleButton(text="{}, {}".format(result[1], result[2]))
                    self.layout_profiles.add_widget(female_btn)
                else:
                    unknown_btn = NoGenderButton(
                        text="{}, {}".format(result[1], result[2]))
                    self.layout_profiles.add_widget(unknown_btn)

【讨论】:

  • 谢谢,这是有道理的。 self.manager.get_screen('information').location.textself.manager.get_screen('information').date 变量会引发错误,因为在应用程序中按下按钮之前它们不会被定义。错误是AttributeError: 'Information' object has no attribute 'location'。我该如何解释?
  • @Callum 为了阅读这个帖子的人着想,最好用minimal reproducible example 创建一个新问题,这样其他人就可以复制你遇到的错误..
  • @Callum 如果你觉得我的回答有帮助,那么你可以考虑accept it,让其他人也看到它.. ;o)
猜你喜欢
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多