0

I'm trying to pass the id of the Button pressed to the move_to_chat() method below so I can evaluate it and create a variable. However at the moment my move_to_chat() method is getting run twice, with different results.

I don't understand why the bind function is calling the move_to_chat function twice, with different results (the second time it produces the result I want)

py file


class ChangedButton(Button):
    dock_id = StringProperty()


class WindowManager(ScreenManager):
    pass

class MyApp(MDApp):

    def on_start(self):
        self.docs = self.my_firestore.db.collection(u'users').where(u'value', u'==', True).stream()
        select_chat = self.root.get_screen("select_chat").ids['select_chat_grid']

        for doc in self.docs: # cycle through values in database and create Buttons for them
            dict = doc.to_dict()
            btn = ChangedButton(text="{} {}".format(dict['first_name'], dict['last_name']), dock_id=doc.id)
            btn.bind(on_release=lambda x:self.move_to_chat(doc.id))
            select_chat.add_widget(btn)

    def move_to_chat(self, doc_id):
        print("logged in user: ", self.local_id)
        print("user to message: ", doc_id)
        group_id = self.local_id + ":"+ doc_id
        print(group_id)
        MDApp.get_running_app().sm.current = "chat"

kv file

<ChangedButton>
    on_release: app.move_to_chat(self.dock_id)
Callum
  • 195
  • 2
  • 22

1 Answers1

1

You have two different lines binding the on_release to your move_to_chat method, therefore when you trigger that event the method gets called twice.

You're also hitting the classic late binding closures thing due to your use of lambda functions in a for loow, which is why the two calls have different arguments.

inclement
  • 29,124
  • 4
  • 48
  • 60