diff --git a/gooey/gui/components/widgets/counter.py b/gooey/gui/components/widgets/counter.py index 766d716..91f095b 100644 --- a/gooey/gui/components/widgets/counter.py +++ b/gooey/gui/components/widgets/counter.py @@ -6,7 +6,8 @@ from gooey.gui import formatters class Counter(Dropdown): def setValue(self, value): - self.widget.SetSelection(value) + index = self._meta['choices'].index(value) + 1 + self.widget.SetSelection(index) def formatOutput(self, metadata, value): return formatters.counter(metadata, value) diff --git a/gooey/python_bindings/argparse_to_json.py b/gooey/python_bindings/argparse_to_json.py index c1dd7a7..5c763fa 100644 --- a/gooey/python_bindings/argparse_to_json.py +++ b/gooey/python_bindings/argparse_to_json.py @@ -331,6 +331,13 @@ def action_to_json(action, widget, options): }, }) + # Issue #321: + # Defaults for choice types must be coerced to strings + # to be able to match the stringified `choices` used by `wx.ComboBox` + default = (str(clean_default(action.default)) + if widget in dropdown_types + else clean_default(action.default)) + return { 'id': action.option_strings[0] if action.option_strings else action.dest, 'type': widget, @@ -343,7 +350,7 @@ def action_to_json(action, widget, options): 'nargs': action.nargs or '', 'commands': action.option_strings, 'choices': list(map(str, action.choices)) if action.choices else [], - 'default': clean_default(action.default), + 'default': default, 'dest': action.dest, }, 'options': merge(base, options.get(action.dest) or {}) @@ -355,6 +362,7 @@ def choose_cli_type(action): if action.required and not action.option_strings \ else 'optional' + def clean_default(default): ''' Attemps to safely coalesce the default value down to diff --git a/gooey/tests/test_argparse_to_json.py b/gooey/tests/test_argparse_to_json.py index 978dbc1..ade8bcd 100644 --- a/gooey/tests/test_argparse_to_json.py +++ b/gooey/tests/test_argparse_to_json.py @@ -53,9 +53,11 @@ class TestArgparse(unittest.TestCase): Issue 321 - must coerce choice types to string to support wx.ComboBox """ parser = ArgumentParser() - parser.add_argument('--foo', choices=[1, 2, 3]) + parser.add_argument('--foo', default=1, choices=[1, 2, 3]) choice_action = parser._actions[-1] result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {}) self.assertEqual(getin(result, ['data', 'choices']), ['1', '2', '3']) + # default value is also converted to a string type + self.assertEqual(getin(result, ['data', 'default']), '1')