easygui.msgbox('你输入' + flavor)TypeError:必须是str,而不是NoneType

时间:2017-03-21 06:23:20

标签: python-3.6 easygui

import easygui

flavour = easygui.enterbox('What is your favourite ice cream flavour?')

easygui.msgbox ('You entered ' + flavour)

我在这里做什么,以便在我点击“取消”时按钮上输入'它不会返回错误?目前我收到以下错误: " easygui.msgbox('你进入' +风味) TypeError:必须是str,而不是NoneType"

2 个答案:

答案 0 :(得分:1)

这样做

import easygui
while True:
flavour = easygui.enterbox('What is your favourite ice cream flavour?(type quit to quit)')
a = bool(flavour)
if a == False:
    easygui.msgbox('you did not enter something')
elif a == True:
    if flavour == 'quit':
        break
    else:
        easygui.msgbox ('You entered ' + flavour)

(我可以让程序更好但更大更大)

答案 1 :(得分:0)

发生的事情是msgbox希望消息是字符串。但是,如果您点击取消,flavourNoneType对象。您可以添加if语句,以确保在单击取消时代码不会出错。做类似的事情:

flavour = easygui.enterbox('What is your favourite ice cream flavour?')

if flavour is not None:
    easygui.msgbox ('You entered ' + str(flavour))
else:
    pass