Python EasyGUI- пределы границ Integerbox
Я использую 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
)
У меня еще ничего нет, потому что я не знаю, что поставить. Любой вклад будет принята с благодарностью. Спасибо!
1 ответ
Когда ничего не помогает, попробуйте прочитать документацию (то есть, если она есть;-).
С EasyGui есть, хотя это отдельная загрузка, easygui-docs-0.97.zip
файл, показанный на этой веб-странице. Вот что написано в разделе API для integerbox()
функция:
Итак, чтобы ответить на ваш вопрос, нет, похоже, нет способа отключить проверку границ модуля. integerbox()
делает.
Обновление: вот новая функция, которую вы можете добавить в модуль, который вообще не выполняет проверку границ и не принимает аргументы границ (поэтому она не является строго совместимой по вызовам со стандартной версией). Если вы вставите его, обязательно добавьте его имя, 'integerbox2'
, к определению __all__
список в верхней части файла скрипта модуля.
Если вы хотите минимизировать изменения в easygui
Сам скрипт модуля на случай будущего обновления, вместо этого вы можете поместить новую функцию в отдельный .py
файл, а затем добавить import integerbox2
в верхней части easygui.py
(плюс еще одна строка, чтобы добавить его в __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