简体   繁体   中英

object of type 'NoneType' has no len()

Classes and other files are found here Do I need to use a lambda function? I usually receive errors along the lines of the immediate below error and the furthest most error which would be " object of type 'NoneType' has no len() "

Traceback (most recent call last):
  File "/home/tech-ministry/dictionary_Merriam-Webster_API/ggwrksv0.03.py", line 32, in <module>
    button_1 = Button(mw, text="Submit", command=print_function())
  File "/home/tech-ministry/dictionary_Merriam-Webster_API/ggwrksv0.03.py", line 21, in print_function
    synonyms = thesaurus.get_synonyms(define_me)
  File "/home/tech-ministry/dictionary_Merriam-Webster_API/py2mwapi.py", line 60, in get_synonyms
    synonyms = self._parse_xml_for_synonyms(result)
  File "/home/tech-ministry/dictionary_Merriam-Webster_API/py2mwapi.py", line 41, in _parse_xml_for_synonyms
    main_entry = self._get_xml_root(xml)
  File "/home/tech-ministry/dictionary_Merriam-Webster_API/py2mwapi.py", line 32, in _get_xml_root
    if not len(first_entry):
TypeError: object of type 'NoneType' has no len()



 from Tkinter import *
#import settings
import py2mwapi

APIKEY = 'f1778399-c8c8-48fd-b6d8-4afd40ce0530'
APIKEY_T = 'a39b602f-93d0-491f-b4e7-2730b9cea4c2'

#################################################

def copytext():
    #print entry_1.get()
    entry_2.delete(0, END)  #remove any previous contents
    entry_2.insert(0, entry_1.get())

def print_function():
    dictionary = py2mwapi.DictionaryAPI(APIKEY)
    thesaurus = py2mwapi.ThesaurusAPI(APIKEY_T)
    define_me = "zeus"

    definition = dictionary.get_definition(define_me)
    synonyms = thesaurus.get_synonyms(define_me)
    related_words = thesaurus.get_related_words(define_me)
    print define_me
#################################################

mw = Tk()
label_1 = Label(mw, text="Enter some text: ")
entry_1 = Entry(mw)
label_2 = Label(mw, text='Output: ')
entry_2 = Entry(mw)

button_1 = Button(mw, text="Submit", command=print_function())



label_1.grid(row=0, column=0, sticky=W)
entry_1.grid(row=0, column=1)
label_2.grid(row=1, column=0, sticky=W)
entry_2.grid(row=1, column=1)
button_1.grid(row=3, columnspan=2, sticky=E)


mainloop()

This was my previous attempt which did not work either:

from Tkinter import *
import py2mwapi
import settings



#################################################

def copytext():
    #print entry_1.get()
    entry_2.delete(0, END)  #remove any previous contents
    entry_2.insert(0, entry_1.get())
#################################################

mw = Tk()

label_1 = Label(mw, text="Enter some text: ")
entry_1 = Entry(mw)
label_2 = Label(mw, text='Output: ')
entry_2 = Entry(mw)
button_1 = Button(mw, text="Submit", command=copytext)

define_me = entry_1.get()

dictionary = py2mwapi.DictionaryAPI(settings.APIKEY)
thesaurus = py2mwapi.ThesaurusAPI(settings.APIKEY_T)

definition = dictionary.get_definition(define_me)
synonyms = thesaurus.get_synonyms(define_me)
related_words = thesaurus.get_related_words(define_me)

label_1.grid(row=0, column=0, sticky=W)
entry_1.grid(row=0, column=1)
label_2.grid(row=1, column=0, sticky=W)
entry_2.grid(row=1, column=1)
button_1.grid(row=3, columnspan=2, sticky=E)


mainloop()

Errors:

Traceback (most recent call last):
  File "/home/tech-ministry/dictionary_Merriam-Webster_API/test.py", line 28, in <module>
    definition = dictionary.get_definition(define_me)
  File "/home/tech-ministry/dictionary_Merriam-Webster_API/py2mwapi.py", line 89, in get_definition
    definition = self._parse_xml_for_def(result)
  File "/home/tech-ministry/dictionary_Merriam-Webster_API/py2mwapi.py", line 77, in _parse_xml_for_def
    main_entry = self._get_xml_root(xml)
  File "/home/tech-ministry/dictionary_Merriam-Webster_API/py2mwapi.py", line 32, in _get_xml_root
    if not len(first_entry):
TypeError: object of type 'NoneType' has no len()

You need to connect the button to a function, not the result of a function. Remove the () .

button_1 = Button(mw, text="Submit", command=print_function)
                                                           ^

The error is telling you what's wrong -- you're trying to call the len function on something that has the value None . You have to figure out why first_entry is set to None .

If it's perfectly valid for first_entry to be None , you can modify your code to handle that case:

if first_entry is not None and len(first_entry):
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM