找到类和其他文件here我是否需要使用lambda函数?我通常会在下面的错误和最远的错误中收到错误,这些错误将是" 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()
这是我之前的尝试,但也无效:
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()
错误:
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()
答案 0 :(得分:1)
您需要将按钮连接到某个功能,而不是功能的结果。删除()
。
button_1 = Button(mw, text="Submit", command=print_function)
^
答案 1 :(得分:1)
错误告诉您错误的原因 - 您尝试在值len
的某些内容上调用None
函数。您必须弄清楚为什么first_entry
设置为None
。
如果first_entry
完全有效None
,您可以修改代码来处理这种情况:
if first_entry is not None and len(first_entry):
...