写这个if语句的更好方法?

2024-04-26 19:00:18 发布

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

我有这个tkinter GUI,我需要从条目中获取值并进行比较。 self.hystInt.get()是访问条目中字符串变量中字符串的方法。*我必须为每一个变量写这个,这样它看起来就很难看了。在

if (self.hystInt.get().isdigit() and int(self.hystInt.get()) >= 200 and int(self.hystInt.get()) <= 500):

Tags: and方法字符串selfgetiftkinter条目
3条回答

做这个。在

try:
    hystInt= int(self.hystInt.get())
    if 200 <= hystInt <= 500:
        Valid.
    else:
        Out of bounds.
except ValueError, e:
    Not even a number.

至少可以使用Python不寻常的比较语法,如下所示:

if (self.hystInt.get().isdigit() and (200 <= int(self.hystInt.get()) <= 500)):
def validate(num):
    try:
        return 200 <= int(num) <= 500
    except ValueError:
        return False

简单就是好!在

相关问题 更多 >