我有一段代码充当按钮的监听器,并评估是否填写了此按钮上方的某些字段:
def goListener(self):
if all( [self.nme.get() != "", self.dsc.get() != "", self.imp.get != ""] ):
name = self.nme.get()
desc = self.dsc.get()
while True:
try:
imp = int(self.imp.get())
break
except:
imp = int(self.imp.get())
当我运行此程序时填入了不同的字段或其他字段,它正确并产生错误消息我要求每个组合除了填写nme和dsc但imp不是,这会产生错误消息:
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1442, in __call__
return self.func(*args)
File "C:\Python33\todo.py", line 68, in goListener
imp = int(self.imp.get())
ValueError: invalid literal for int() with base 10: ''
顺便说一下,这是运行except块,它不应该。这是评估的问题,还是我在这里遗漏了什么?
答案 0 :(得分:4)
你有:
self.imp.get != ""
您未能调用.get()
方法。尝试:
self.imp.get() != ""
答案 1 :(得分:3)
如果imp = int(self.imp.get())
抛出错误,在try
块之外再次调用它将会抛出相同的错误。
except
块用于在出现异常时应运行的代码:
try:
imp = int(self.imp.get())
break
except:
print "Something bad happened"