#AttributeError: 'NoneType' object has no attribute ... Exampletry: # In order to be able to import tkinter for import tkinter as tk # either in python 2 or in python 3except ImportError: import Tkinter as tkroot = tk.Tk()widget = tk.Label(root, text="Label 1").grid()widget.config(text="Label A")root.mainloop()上面的代码产生错误:Traceback (most recent call last): File "C:\Users\user\Documents\Python\other\script.py", line 8, in <module> widget.config(text="Label A")AttributeError: 'NoneType' object has no attribute 'config'类似地,代码片段:#TypeError: 'NoneType' object does not support item assignment Exampletry: # In order to be able to import tkinter for import tkinter as tk # either in python 2 or in python 3except ImportError: import Tkinter as tkroot = tk.Tk()widget = tk.Button(root, text="Quit").pack()widget['command'] = root.destroyroot.mainloop()产生错误:Traceback (most recent call last): File "C:\Users\user\Documents\Python\other\script2.py", line 8, in <module> widget['command'] = root.destroyTypeError: 'NoneType' object does not support item assignment在两种情况下:>>>print(widget)None为什么这样,为什么widget存储为None,或者为什么在尝试配置窗口小部件时出现上述错误?这个问题是基于此,并问了广义回答很多关于这个主题相关的和重复的问题。参见此以拒绝编辑。
添加回答
举报
0/150
提交
取消