easygui icon indicating copy to clipboard operation
easygui copied to clipboard

Enhancement: separate choice text and choice values

Open spyoungtech opened this issue 8 years ago • 3 comments

I'm proposing an enhancement to allow the separation of choice text from the value returned by selecting the choice by passing in a dict to the choices parameter where the choice text is the key and the value is the return val (e.g. {choice_text: ret_val} or {"No": False} )

Currently, the value returned by a buttonbox or choicebox is the same as the button text. But suppose I want the text to display one thing, but the value to be returned as something else. The boolbox (and its derivatives, like ynbox ccbox etc) is sort of an example of this: the button texts are Yes/No but return True/False respectively.

The enhancement I'm proposing would allow someone to pass in a dict for the choices parameter for boxes that accept a choices parameter.

For example,

import easygui

#CCbox
easygui.buttonbox(msg="Shall I continue", choices={"C[o]ntinue": True, "C[a]ncel": False})
#Another example
easygui.buttonbox(msg="Do you identify as a protected veteran?", 
                  choices={"I am a protected veteran": True, 
                            "I am NOT a protected veteran": False, 
                            "I do not wish to answer": None}
)

This is particularly helpful when you want to fix typos or change the text of options without having to change your program logic. Additionally, this will help avoid troublesome patterns like the following:

#Bad things can happen if you want to change the text of choices if using these common patterns.
response = easygui.buttonbox(msg=msg, choices=choices) 
if response == "The exact text of the affirmative response": 
    the_var = True
#or
response_table = {"The exact text of the negative response": False,
                  "The exact text of the affirmative response": True,
                  "The exact text of an ambiguous response": None
}
the_var = response_table[response]

This enhancement I'm proposing will help avoiding problems with such patterns or avoid the use of such patterns altogether. I personally encountered this a lot in polishing my easygui apps where someone wants the button to say something slightly different or fix a typo or something of that nature, usually introducing bugs.

Passing in dicts with key-value pairs of {choice_text: ret_val} can be implemented without it being a breaking change. (Currently, passing in a dict results in an AttributeError)

Hopefully this is clear enough to understand. I'll be working on developing the solution for this and will update here or submit a pull request; whatever is appropriate.

spyoungtech avatar Sep 26 '16 19:09 spyoungtech

Go for it !!

jjdenis avatar Sep 26 '16 21:09 jjdenis

@jjdenis thanks. I think I've worked up a good solution for this. What's the best way for me to share the contribution? Should I just make a pull request to the master branch? Or something else?

Thanks!

Edit: You can see 2f21c8b from my fork for the changes.

spyoungtech avatar Sep 26 '16 23:09 spyoungtech

A pull request would be just fine. Thank you very much.

Juan José Denis Corrales

El 27 sept 2016, a las 1:33, Spencer Young [email protected] escribió:

@jjdenis thanks. I think I've worked up a good solution for this. What's the best way for me to share the contribution? Should I just make a pull request to the master branch? Or something else?

Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

jjdenis avatar Sep 27 '16 04:09 jjdenis