当前位置:   article > 正文

Python-tkinter 'NoneType' object has no attribute 'config'_attributeerror: 'nonetype' object has no attribute

attributeerror: 'nonetype' object has no attribute 'configure

刚开始学习PythonGUI编程,遇到这种问题

当创建一个对象后,使用.config()函数为它设置参数,比如

  1. Label_1 = Label(frame_1).pack(side='right')
  2. def submit():
  3. Label_1.configure(text=var2)

很简单的三行代码,但是当运行时,却会报错

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

原因是,当你创建一个Label对象后,马上调用了pack方法,然后pack()返回None,所以接下来再执行config()会报错

正确的作法:

  1. Label_1 = Label(frame_1)
  2. Label_1.pack(side='right')
  3. def submit():
  4. Label_1.configure(text=var2)

即,先创建对象,再执行其他的操作

类比,这种做法对于其他的对象类型,如Button,Radiobutton,Entry……以及放置函数grid(),place()均适用

 

return 0;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/759085
推荐阅读
相关标签
  

闽ICP备14008679号