[pygtk] Need ToolButton to only be sensitive when TreeView has a selection

Robert Park rbpark at exolucere.ca
Wed Sep 1 08:42:03 WST 2010


I realized I had two buttons that both need to be unsensitive when
there's no selection, and having two connect lines each making their
own lambda seemed really inefficient, so I refactored it into this:

def selection_changed(self, selection, data=None):
	sensitivity = selection.count_selected_rows() > 0
	self.apply_button.set_sensitive(sensitivity)
	self.delete_button.set_sensitive(sensitivity)

...

self.treeview.get_selection().connect("changed", self.selection_changed)

Thanks!


On Tue, Aug 31, 2010 at 3:43 AM, Timo <timomlists at gmail.com> wrote:
> I always do it like this:
>
> self.selection = self.treeview.get_selection()
> self.selection.connect('changed', self.selection_changed)
>
> def selection_changed(self, selection):
>    model, tree_iter = selection.get_selected()
>    if tree_iter is None:
>        self.apply_button.set_sensitive(False)
>    else:
>        self.apply_button.set_sensitive(True)


-- 
http://exolucere.ca


More information about the pygtk mailing list