Python EasyGUI整合器边界限制

2024-09-22 16:28:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用EasyGUI作为我正在编写的一个小程序的一部分。在它中,我使用的是IntegerBox“函数”。在

此函数的部分参数是下限和上限(输入值的限制)。如果该值低于下限值或超过上限值,则程序将引发错误。在

只有在这个程序中,我想删除下界/上界,这样就可以输入任何数字。在

我的代码是:

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
                   )

我还没有输入任何东西,因为我不知道该放什么。如有任何意见,我们将不胜感激。谢谢!在


Tags: the函数代码import程序none参数错误
1条回答
网友
1楼 · 发布于 2024-09-22 16:28:28

当所有其他方法都失败时,尝试读取文档(也就是说,如果有的话;-)。在

EasyGui有一个单独的下载文件easygui-docs-0.97.zip,显示在webpage上。下面是它在integerbox()函数的API部分中所说的:

screenshot of easygui integer box documentation

因此,为了回答您的问题,no,似乎没有一种方法可以禁用对模块integerbox()所做的边界检查。在

更新:这里有一个新函数可以添加到模块中,该函数根本不进行边界检查,也不接受边界参数(因此它与普通版本不严格调用兼容)。如果将其放入,请确保将其名称'integerbox2'添加到模块脚本文件顶部附近的__all__列表的定义中。在

如果您希望最小化对easygui模块脚本本身的更改,以防将来有更新,您可以将新函数放在一个单独的.py文件中,然后在easygui.py顶部附近添加一个import integerbox2(加上另一行将其添加到__all__)中。在

以下是附加功能:

#                                 -
# 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

相关问题 更多 >