用Tkinter的组件时遇到,无法摧毁/隐藏/放置的情况
AttributeError: ‘NoneType’ object has no attribute ‘place’
AttributeError: ‘NoneType’ object has no attribute ‘place_forget’
AttributeError: ‘NoneType’ object has no attribute ‘destroy’
出现这个错误的原因,举个例子:
# 写法一:c = a().b()式写法
# 由于 pack(), place(), grid() 的返回值是'None'
# 这样 self.subBar 得到的返回值是 'None',
# 而不是我们希望的 a 的返回值
# 执行到 'destroy()'的时候,就会出以下错误
# AttributeError: 'NoneType' object has no attribute 'destroy'
self.subBar = Frame(self.backGround,bg='#FFFFFF').place(x=10,y=75,width=100,height=5)
.....
.....
self.subBar.destroy()
# 写法二:
# c = a.()
# c.b()
# 这样 c 得到的是 a 的返回值,
# 后面的 'destroy()' 就可以正常执行了
self.subBar = Frame(self.backGround,bg='#FFFFFF')
self.subBar.place(x=10,y=75,width=100,height=5)
.....
.....
self.subBar.destroy()
忽略了 place() 的赋值。。。
版权声明:本文为weixin_44011353原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。