hehe210417 发表于 2020-4-29 20:19:34

easygui使用问题

import easygui as g
f = g.enterbox()
if f.isdigit():
      g.msgbox('您输入的是整数')

在enterbox中点击取消按钮的话,就会报错,说'NoneType' object has no attribute 'isdigit'
就是想知道点击取消之后,enterbox返回的是什么啊

liuzhengyuan 发表于 2020-4-29 20:23:12

本帖最后由 liuzhengyuan 于 2020-4-29 20:25 编辑

中途输出一下

import easygui as g
f = g.enterbox()
print(f)
if f.isdigit():
      g.msgbox('您输入的是整数')
输出 None

此时它没有数据类型(None Type),所以会这样。。。

加一个判断是否为 None 的 if 就可以了
https://www.cnblogs.com/lincappu/p/8305763.html

hehe210417 发表于 2020-4-29 20:25:45

就是点击取消的话,会报错啊,就是None没有isdigit属性,怎么让它不报错

liuzhengyuan 发表于 2020-4-29 20:26:08

hehe210417 发表于 2020-4-29 20:25
就是点击取消的话,会报错啊,就是None没有isdigit属性,怎么让它不报错

加一个判断是否为 None 的 if 就可以了
https://www.cnblogs.com/lincappu/p/8305763.html

永恒的蓝色梦想 发表于 2020-4-29 20:32:22

import easygui as g
f = g.enterbox()
if f and f.isdigit():
      g.msgbox('您输入的是整数')这样就可以了

zltzlt 发表于 2020-4-29 20:41:47

当用户按下取消按钮的时候 enterbox() 会返回 None,加一个判断即可。

import easygui as g

f = g.enterbox()
if f is not None:# 如果 f 不为 None
    if f.isdigit():
      g.msgbox('您输入的是整数')

hehe210417 发表于 2020-4-29 20:42:35

import easygui as g
f = g.enterbox()
if f.isdigit():
      g.msgbox('您输入的是整数')
else:
      g.enterbox('输入有误,请输入一个整数')

想实现这个目的,就是做之前的那个闰年判断器,首先判断是不是输入了整数,是的话进行判断,不是的话重新输入,点击enterbox的取消按钮就能退出

永恒的蓝色梦想 发表于 2020-4-29 20:42:42

zltzlt 发表于 2020-4-29 20:40
有 Bug

举个例子?

zltzlt 发表于 2020-4-29 20:43:56

永恒的蓝色梦想 发表于 2020-4-29 20:42
举个例子?

噢,一时想错了{:10_266:}

txxcat 发表于 2020-4-29 20:47:37

输入整数完全没有必要用enterbox(),因为有这个:integerbox(msg='', title=' ', default=None, lowerbound=0, upperbound=99, image=None, root=None)。
如果一定要用enterbox():
import easygui as g
f = g.enterbox()
if f and f.isdigit():
   g.msgbox('您输入的是整数')

hehe210417 发表于 2020-4-29 20:48:16

加上and进行判断的话,如果输入的是字符串,就直接退出了

liuzhengyuan 发表于 2020-4-29 20:49:02

hehe210417 发表于 2020-4-29 20:48
加上and进行判断的话,如果输入的是字符串,就直接退出了

加个 else 呗,
import easygui as g
f = g.enterbox()
print(f)
if f.isdigit() and f is not None:
      g.msgbox('您输入的是整数')
else:
      g.msgbox('不是整数')

txxcat 发表于 2020-4-29 20:52:05

呃,代码和5楼一样,其实这个是没有bug的,python在判断条件的时候,如果是and关系,第一个条件为False就不会验证第二个条件,所以不会触发错误,如果反过来if f.isdigit() and f就不行了。

txxcat 发表于 2020-4-29 20:57:26

liuzhengyuan 发表于 2020-4-29 20:47


代码有问题,cancel或关闭窗口就报错了。

txxcat 发表于 2020-4-29 21:01:43

hehe210417 发表于 2020-4-29 20:42
import easygui as g
f = g.enterbox()
if f.isdigit():


import easygui as g
f = g.enterbox()
if f and f.isdigit():
   g.msgbox('您输入的是整数')
elif f:
   g.msgbox('输入有误,请输入一个整数')
#这样写按cancel或关闭窗口就直接退出,有输入就会判断,不过,用integerbox()不好吗?

永恒的蓝色梦想 发表于 2020-4-29 21:07:17

hehe210417 发表于 2020-4-29 20:48
加上and进行判断的话,如果输入的是字符串,就直接退出了

但是如果不是整数也会直接退出啊
页: [1]
查看完整版本: easygui使用问题