/GUI/Generic/GRadioGroups.py

https://bitbucket.org/alsh/pygui-mirror · Python · 79 lines · 73 code · 2 blank · 4 comment · 0 complexity · b1de5b96b21c0b25bd7d1eee019db89d MD5 · raw file

  1. #
  2. # Python GUI - Radio groups - Generic
  3. #
  4. from Properties import Properties, overridable_property
  5. from Actions import Action
  6. class RadioGroup(Properties, Action):
  7. """A RadioGroup coordinates a group of RadioButtons.
  8. It has a 'value' property which is equal to the value
  9. of the currently selected RadioButton. It may be given
  10. an action procedure to execute when its value changes.
  11. Operations:
  12. iter(group)
  13. Returns an iterator over the items of the group.
  14. """
  15. value = overridable_property('value', """The value of the currently
  16. selected radio button.""")
  17. _items = None
  18. _value = None
  19. def __init__(self, items = [], **kwds):
  20. Properties.__init__(self, **kwds)
  21. self._items = []
  22. self.add(*items)
  23. #
  24. # Operations
  25. #
  26. def __iter__(self):
  27. return iter(self._items)
  28. #
  29. # Properties
  30. #
  31. def get_value(self):
  32. return self._value
  33. def set_value(self, x):
  34. if self._value <> x:
  35. self._value = x
  36. self._value_changed()
  37. self.do_action()
  38. #
  39. # Adding and removing items
  40. #
  41. def add(self, *items):
  42. "Add one or more RadioButtons to this group."
  43. for item in items:
  44. item.group = self
  45. def remove(self, *items):
  46. "Remove one or more RadioButtons from this group."
  47. for item in items:
  48. item.group = None
  49. def _add(self, item):
  50. self._items.append(item)
  51. self._item_added(item)
  52. def _remove(self, item):
  53. self._items.remove(item)
  54. self._item_removed(item)
  55. def _item_added(self, item):
  56. raise NotImplementedError
  57. def _item_removed(self, item):
  58. raise NotImplementedError
  59. def _value_changed(self):
  60. raise NotImplementedError