Python EasyGUI- Integerbox Bound Limits

无人久伴 提交于 2019-12-11 12:36:40

问题


I am using EasyGUI as part of a small program I am writing. In it, I am using the IntegerBox "function".

Part of the parameters of this function is the lowerbound and the upperbound (the limits of the value entered). If the value is under the lowerbound or if it exceeds the upperbound, the program raises an error.

Only for this program, I want to remove the lowerbound/upperbound--- so it is possible to put any number in.

My code is:

import easygui as eg
numMin=eg.integerbox(msg="What is the minimum value of the numbers?"
                   , title="Random Number Generator"
                   , default=0
                   , lowerbound=
                   , upperbound=
                   , image=None
                   , root=None
                   )

I don't have anything entered yet, because I don't know what to put. Any input would be greatly appreciated. Thanks!


回答1:


When all else fails, trying reading the documentation (that is, if there is any ;-).

With EasyGui there is, although it's a separate download, the easygui-docs-0.97.zip file, shown on this webpage. Here's what it says in the API section for the integerbox() function:

So, to answer your question, no, there doesn't appear to be a way to disable the bounds-checking the module's integerbox() does.

Update: Here's a new function you can add to the module that doesn't do bounds-checking at all nor does it accept bounds arguments (so it's not strictly call-compatible with the stock version). If you put it in, be sure to also add its name, 'integerbox2', to the definition of the __all__ list near the top of the module's script file.

If you would like to minimize changes to the easygui module's script itself in case there's a future update, you instead could put the new function in a separate .py file and then add an import integerbox2 near the top of easygui.py (plus another line to add it to __all__).

Here's the additional function:

#-------------------------------------------------------------------
# integerbox2 - like integerbox(), but without bounds checking.
#-------------------------------------------------------------------
def integerbox2(msg=""
               , title=" "
               , default=""
               , image=None
               , root=None):
    """
    Show a box in which a user can enter an integer.

    In addition to arguments for msg and title, this function accepts
    an integer argument for "default".

    The default argument may be None.

    When the user enters some text, the text is checked to verify that it
    can be converted to an integer, **no bounds checking is done**.

    If it can be, the integer (not the text) is returned.

    If it cannot, then an error msg is displayed, and the integerbox is
    redisplayed.

    If the user cancels the operation, None is returned.

    :param str msg: the msg to be displayed
    :param str title: the window title
    :param str default: The default value to return
    :param str image: Filename of image to display
    :param tk_widget root: Top-level Tk widget
    :return: the integer value entered by the user

    """
    if not msg:
        msg = "Enter an integer value"

    # Validate the arguments and convert to integers
    exception_string = ('integerbox "{0}" must be an integer.  '
                        'It is >{1}< of type {2}')
    if default:
        try:
            default=int(default)
        except ValueError:
            raise ValueError(exception_string.format('default', default,
                                                     type(default)))

    while 1:
        reply = enterbox(msg, title, str(default), image=image, root=root)
        if reply is None:
            return None
        try:
            reply = int(reply)
        except:
            msgbox('The value that you entered:\n\t"{}"\n'
                   'is not an integer.'.format(reply), "Error")
            continue
        # reply has passed validation check, it is an integer.
        return reply


来源:https://stackoverflow.com/questions/13366395/python-easygui-integerbox-bound-limits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!