Search code examples
pythonclasskivyinstance

Why do you need the 'instance' variable when creating a method in Kivy?


I am currently learning the basics of Kivy, and I have noticed that you need 2 arguments to create a button method: 'self' (which I would expect) and 'instance' (although I assume this could be any variable name). Why do you need this second variable 'instance' as an argument for the button method?

    self.submit = Button(text = "Submit", font_size = 40)
    self.submit.bind(on_press = self.button_pressed)
    self.add_widget(self.submit)

    
def button_pressed(self, instance):
    FirstName = self.FirstName.text
    LastName = self.LastName.text
    Email = self.Email.text

Solution

  • It looks like you're following along with something similar to the guide at (https://www.techwithtim.net/tutorials/kivy-tutorial/creating-buttons-triggering-events/)

    In this case, "self" is an instance of the "MyGrid" object that contains, as children, the FirstName, LastName, and Email input objects. "instance" is then a reference to the button that was pressed, in this case, the "submit" button.