0

I'm trying to get a Screen initialised with a number of Buttons depending on the result from a query to my database but I'm getting an AttributeError. I think this is because when I initialise my app there is no Information Screen and reading this answer: AttributeError: 'NoneType' object has no attribute 'current' I need to use Clock to delay the initialisation of my Finder class so my Information class has time to be created but I'm not quite sure how I go about doing this in the __init__ method, or if this is even the right approach?

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)

Error

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

1 Answers1

0

, dtThis is a way to execute all the stuff after the screen's initialization

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)
noEmbryo
  • 2,176
  • 2
  • 10
  • 15
  • Thanks, this makes sense. the ```self.manager.get_screen('information').location.text``` and ```self.manager.get_screen('information').date``` variables are throwing an error as they don't get defined until a button is pressed in the app. The error is ```AttributeError: 'Information' object has no attribute 'location'```. How do I account for this? – Callum Feb 16 '20 at 21:50
  • @Callum For the sake of the people who will read this thread, it is better to create a new question with a [mcve], so others can duplicate the error you are encounter.. – noEmbryo Feb 16 '20 at 22:39
  • @Callum If you found my answer helpful, then you might consider to [accept it](https://stackoverflow.com/help/someone-answers), so other people see it too.. ;o) – noEmbryo May 29 '20 at 09:50