Question

The sample code is like this:

class Something(gtk.Window):
    def __init__(self):
        ...
        treeview = gtk.TreeView(store)

        tree_selection = treeview.get_selection()                               
        tree_selection.set_mode(gtk.SELECTION_SINGLE)                           
        tree_selection.connect("changed", self.onSelectionChanged)
        ...

     def onSelectionChanged(self, tree_selection):                               
        (model, pathlist) = tree_selection.get_selected()                       
        self.selectedValue = model.get_value(pathlist, 0) - 1

How was the tree_selection passed in into onSelectionChanged function? I see many other similar uses, such as:

def onDestroy(self, widget):
    gtk.main_quit()

What can we use this "widget" in the second param?

Was it helpful?

Solution

When you connect a handler (like onSelectionChanged) to a signal (like "changed"), the handler is added to the end of the list of signal handlers of that object.

When the object then emits the signal, it will call handlers (or "callbacks") in this list, pasing itself as the first argument.

This is true for all signals: the first argument is allways the object emitting the signal.


This way, you can e.g. call the get_selected() method of the TreeSelection object that called your onSelectionChanged method: you access it through the tree_selection parameter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top