GtkSelectionList.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import gi
  2. gi.require_version('Gtk', '3.0')
  3. from gi.repository import Gtk
  4. class GtkSelectionList(Gtk.ListBox):
  5. def __init__(self, *args, **kwds):
  6. super().__init__(*args, **kwds)
  7. self.connect("row-selected", self.on_selection)
  8. self.selections={}
  9. self._on_selected=None
  10. self.current_selection_name=""
  11. def init(self):
  12. ch=self.get_children()
  13. for c in ch: super().remove(c)
  14. self.current_selection_name=""
  15. def set_on_selected(self, a):
  16. self._on_selected=a
  17. def rename(self, old, new):
  18. self.selections[new]=self.selections[old]
  19. del self.selections[old]
  20. self.selections[new][0].set_text(new)
  21. def append(self, name, sel):
  22. label = Gtk.Label.new(name)
  23. label.set_alignment(0,0)
  24. self.add(label)
  25. label.show_all()
  26. self.selections[name]=(label, sel)
  27. def remove(self, name):
  28. if name in self.selections:
  29. lbl = self.selections[name][0]
  30. for i in range(len(self.selections)):
  31. row = self.get_row_at_index(i)
  32. label = row.get_child()
  33. if label==lbl:
  34. super().remove(row)
  35. del self.selections[name]
  36. def on_selection(self, obj, c):
  37. for k in self.selections:
  38. if c.get_child()==self.selections[k][0]:
  39. self.current_selection_name=k
  40. self._on_selected(k, self.selections[k][1])