Kivy AttributeError:“ NoneType”对象没有属性“ get_screen”

时间:2020-02-16 20:31:39

标签: python kivy

我正在尝试根据对数据库的查询结果,以一个Screen初始化一个Buttons,但是我得到一个AttributeError。我认为这是因为初始化我的应用程序时,没有Information Screen并没有阅读以下答案:AttributeError: 'NoneType' object has no attribute 'current' 我需要使用Clock来延迟Finder类的初始化,因此我的Information类有时间创建,但是我不太确定如何在{ {1}}方法还是正确的方法?

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

1 个答案:

答案 0 :(得分:0)

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