天天看點

AttributeError: 'NoneType' object has no attribute 'insert'

1. pack()錯誤
pack()會傳回None值,類型為<class NoneType>,類型錯誤,空值類型,沒有其他類型的屬性
def createWidget(self):
    self.text1 = Text(self, width=40, height=12, bg='gray').pack() #此處會将空值類型賦給 self.text1
    #self.text1.pack()
    #self.text1.insert(1.0, '0123456789\nabcdefg')
    #self.text1.insert(2.3, '鋤禾日當午,汗滴禾下土,誰知盤中餐,粒粒皆辛苦。\n')
    print(type(self.text1))      

2. 擴充

其他提示<NonoType>類型錯誤的,也可能是被引用的變量,變成了空值類型,注意其指派代碼

如:AttributeError: 'NoneType' object has no attribute 'get'

       a=a.append('b') #append()傳回空值None,為NoneType