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.

75 lines
2.3 KiB

2 years ago
2 years ago
2 years ago
2 years ago
  1. import wx # type: ignore
  2. from gooey.gui import formatters
  3. from gooey.gui.components.widgets.bases import TextContainer
  4. from gooey.python_bindings import types as t
  5. class IntegerField(TextContainer):
  6. """
  7. An integer input field
  8. """
  9. widget_class = wx.SpinCtrl
  10. def getWidget(self, *args, **options):
  11. widget = self.widget_class(self,
  12. value='',
  13. min=self._options.get('min', 0),
  14. max=self._options.get('max', 100))
  15. return widget
  16. def getWidgetValue(self):
  17. return self.widget.GetValue()
  18. def setValue(self, value):
  19. self.widget.SetValue(int(value))
  20. def formatOutput(self, metatdata, value):
  21. # casting to string so that the generic formatter
  22. # doesn't treat 0 as false/None
  23. return formatters.general(metatdata, str(value))
  24. def getUiState(self) -> t.FormField:
  25. widget: wx.SpinCtrl = self.widget
  26. return t.IntegerField(
  27. id=self._id,
  28. type=self.widgetInfo['type'],
  29. value=self.getWidgetValue(),
  30. min=widget.GetMin(),
  31. max=widget.GetMax(),
  32. error=self.error.GetLabel() or None,
  33. enabled=self.IsEnabled(),
  34. visible=self.IsShown()
  35. )
  36. class DecimalField(IntegerField):
  37. """
  38. A decimal input field
  39. """
  40. widget_class = wx.SpinCtrlDouble
  41. def getWidget(self, *args, **options):
  42. widget = self.widget_class(self,
  43. value='',
  44. min=self._options.get('min', 0),
  45. max=self._options.get('max', 100),
  46. inc=self._options.get('increment', 0.01))
  47. widget.SetDigits(self._options.get('precision', widget.GetDigits()))
  48. return widget
  49. def setValue(self, value):
  50. self.widget.SetValue(value)
  51. def getUiState(self) -> t.FormField:
  52. widget: wx.SpinCtrlDouble = self.widget
  53. return t.IntegerField(
  54. id=self._id,
  55. type=self.widgetInfo['type'],
  56. value=self.getWidgetValue(),
  57. min=widget.GetMin(),
  58. max=widget.GetMax(),
  59. error=self.error.GetLabel() or None,
  60. enabled=self.IsEnabled(),
  61. visible=self.IsShown()
  62. )