Python EasyGUI- Integerbox绑定限制 [英] Python EasyGUI- Integerbox Bound Limits

查看:374
本文介绍了Python EasyGUI- Integerbox绑定限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EasyGUI作为编写的小程序的一部分.在其中,我正在使用IntegerBox功能".

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.

我的代码是:

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!

推荐答案

如果其他所有方法均失败,请尝试阅读文档(即,是否有;-).

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

使用EasyGui可以下载easygui-docs-0.97.zip文件,尽管它是单独下载的文件,该文件显示在

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:

因此,要回答您的问题,,似乎没有一种方法可以禁用边界检查模块的integerbox()功能.

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

更新:这是一个新功能,您可以添加到完全不进行边界检查也不接受边界参数的模块(因此,它并不严格与库存版本通话兼容).如果将其放入,请确保还将其名称'integerbox2'添加到模块脚本文件顶部附近的__all__列表的定义中.

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.

如果要在将来进行更新的情况下最小化对easygui模块脚本本身的更改,可以将新功能放在单独的.py文件中,然后在easygui顶部附近添加import integerbox2 easygui.py(加上另一行将其添加到__all__).

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__).

这是附加功能:

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

这篇关于Python EasyGUI- Integerbox绑定限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆