为了账号安全,请及时绑定邮箱和手机立即绑定

为什么Tkinter小部件存储为None?(AttributeError:“ NoneType”

为什么Tkinter小部件存储为None?(AttributeError:“ NoneType”

PIPIONE 2019-12-06 15:42:20
#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,或者为什么在尝试配置窗口小部件时出现上述错误?这个问题是基于此,并问了广义回答很多关于这个主题相关的和重复的问题。参见此以拒绝编辑。
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 413 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信