¿Por qué el widget Tkinter se almacena como Ninguno? (AttributeError: objeto 'NoneType' ...) (TypeError: objeto 'NoneType' ...) [duplicado]
Esta pregunta ya tiene una respuesta aquí:
¿Por qué mis widgets de Tkinter se almacenan como Ninguno? [duplicar] 1 respuesta#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()
El código anterior produce el error:
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'
Del mismo modo, la pieza de código:
#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()
produce el error:
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
Y en ambos casos:
>>>print(widget)
None
Por qué es eso, por qué eswidget
almacenado comoNone
, o ¿por qué recibo los errores anteriores cuando intento configurar mis widgets?
Esta pregunta se basa enesta y se le pide una respuesta generalizada a muchas preguntas relacionadas y repetitivas sobre el tema. Veresta para editar el rechazo.