0

I am learning Gtk programming with python from this. But I can not understand what is the reason for the widget keyword in the following callback definition? Can someone please explain what it does?

def on_button_clicked(self, widget):
    print("Hello World")
Dipanjan Patra
  • 59
  • 1
  • 3
  • 10

1 Answers1

2

When the button is clicked, the on_button_clicked method is executed. When this happens, the window and button objects are passed to the method as self and widget, respectively. This is useful in cases where one needs to do something with the button, such as getting its state or changing its text.

Without the widget parameter, the button object would be passed to the method and there wouldn't be a parameter to receive it, and you would get a TypeError.

jmcampbell
  • 378
  • 4
  • 17
  • does that mean the instance of any object is always passed to all the callback functions which are called from that instance? – Dipanjan Patra Jul 19 '17 at 18:36
  • @DipanjanPatra Yes, that is why the first parameter of an object's method is always self. – jmcampbell Jul 19 '17 at 20:38
  • I'm not very familiar with PyGObject; does it not provide a way to pass callback data (which would be a third parameter)? – andlabs Jul 20 '17 at 04:29