【发布时间】:2018-05-17 01:48:05
【问题描述】:
#AttributeError: 'NoneType' object has no attribute ... Example
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
root = 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 Example
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
root = tk.Tk()
widget = tk.Button(root, text="Quit").pack()
widget['command'] = root.destroy
root.mainloop()
产生错误:
Traceback (most recent call last): File "C:\Users\user\Documents\Python\other\script2.py", line 8, in <module> widget['command'] = root.destroy TypeError: 'NoneType' object does not support item assignment
在这两种情况下:
>>>print(widget)
None
为什么会这样,为什么widget 存储为None,或者为什么我在尝试配置小部件时会收到上述错误?
【问题讨论】:
标签: python tkinter widget attributeerror nonetype