Closed spyoungtech closed 8 years ago
Gotcha. I originally did just that, but ultimately decided to put it up at the function mainly for readability and simplicity. I'll make the change and implement it in the ButtonBox/ChoiceBox classes.
@jjdenis I pushed those changes there. Check it out and let me know what you think.
Hello @spyoungtech ! I have pushed your changes to the branch called 'develop'.
I have also done some refactoring so the code is easier to read, modify and, hopefully, re-refactor.
I have also streamlined and demoed its use with callbacks.
Feedback would be greatly appreciated, @robertlugg.
I have tested the code in linux, but testing in osx and windows would be greatly appreciated too.
This enhancement makes a change to the
buttonbox
choicebox
andmultchoicebox
functions so that a dict-like object can be passed into thechoices
parameter, where the keys represent the text of the choice to be displayed and the corresponding values are returned when that choice is selected, I.E.choices = {choice_text: return_value}
See #111 for more detail.This change should make it easier to get the desired values returned from dialogs, rather than needing to convert responses into appropriate values. It also allows for changing the button text and hotkeys easily, without altering the returned value and subsequent programming logic. (see #111 for more detail)
One caveat is that dictionaries do not preserve order. So if choices need to preserve order, an OrderedDict should be used. In Python 3.6 all dicts will be ordered, but OrderedDict should still be used for backwards compatibility.
The following is an example that uses this feature to replicate
ccbox
andindexbox
boxes.If
cancel_choice
is received in a buttonbox reply, ifcancel_choice
is a key in thechoices
dict, the value ofchoices[reply]
will be returned, elsecancel_choice
is returned. Similarly, ifNone
is received by a choicebox or multchoicebox, ifNone
is a key inchoices
choices[None]
is returned, otherwiseNone
is returned. I believe this is a sensible behavior, but am not opposed to changes.If accepted, the docstrings for these functions should reflect the change and demos should be provided. I can push that change out as well if desired.