You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.4 KiB

  1. from gooey.gui.components.widgets.bases import TextContainer
  2. import wx
  3. from gooey.gui import formatters
  4. from gooey.gui.lang.i18n import _
  5. class Dropdown(TextContainer):
  6. def getWidget(self, parent, *args, **options):
  7. default = _('select_option')
  8. return wx.ComboBox(
  9. parent=parent,
  10. id=-1,
  11. # str conversion allows using stringyfiable values in addition to pure strings
  12. value=str(default),
  13. choices=[str(default)] + [str(choice) for choice in self._meta['choices']],
  14. style=wx.CB_DROPDOWN)
  15. def setOptions(self, options):
  16. prevSelection = self.widget.GetSelection()
  17. self.widget.Clear()
  18. for option in [_('select_option')] + options:
  19. self.widget.Append(option)
  20. self.widget.SetSelection(0)
  21. def setValue(self, value):
  22. ## +1 to offset the default placeholder value
  23. index = self._meta['choices'].index(value) + 1
  24. self.widget.SetSelection(index)
  25. def getWidgetValue(self):
  26. value = self.widget.GetValue()
  27. # filter out the extra default option that's
  28. # appended during creation
  29. if value == _('select_option'):
  30. return None
  31. return value
  32. def formatOutput(self, metadata, value):
  33. return formatters.dropdown(metadata, value)