使用tkinter调用函数时的TypeError

时间:2015-07-24 08:48:28

标签: python-3.x tkinter

我从ttkinter按钮

调用下面的两个函数
search_btn = ttk.Button(actions_frame, text = 'SEARCH TERM', command=lambda: get_matches() & choose_image(photo_image, label_text, label_image))

和功能:

def get_matches(): # programmed by Ryan Carr, modified me, all mistakes, mine!
        ''' Compares users input to dictionary entries
        '''
        lst = []
        #with open('enciclopedia_nautica.txt', "rb") as en_outfile: #for italian dictionary
        with open('english_french_italian.txt', 'rb') as en_outfile: # full dictionary
        #with open('english_nautical.txt', "rb") as en_outfile: # for bilingual dictionary
            english_dict = pickle.loads(en_outfile.read()) # load dictionary from text
        inp = e.get()
        # Grabs a tuple containing key, value pair from dictionary
        for item in english_dict.items():
            # Look at both the key and value in the tuple
            for value in item:
                if inp in value:
                    # This prevents duplicate entries
                    # For example typing in apple would give two answers.
                    if item[0] not in lst:
                        # If it exists, append the related key to the list
                        lst.append(item[0])

        # If our list of keys is still empty the string wasn't found
        if len(lst) == 0:
            statement = '\nthe term "'"{0}"'" is not in the dictionary\n'.format(inp)
            translation_field.insert(END, statement)

        for item in lst:
            result = "{0} = {1}\n".format(item, english_dict[item])
            translation_field.insert(END,result)


    def choose_image(photo_image, text_lab, image_lab):
            with open('./images/final_image_dict.txt', 'rb') as outfile:
                library = pickle.loads(outfile.read())

            term = e.get()
            if term == "":
                photo_image.config(file="./images/hello_tkinter_cartoon.gif")
                text_lab.config(text="enter search term")
                image_lab.config(image=photo_image)
            else:
                for key, value in library.items():
                    if term.lstrip()  in key:
                        print(value[1])
                        photo_image.config(file=value[1].lstrip())
                        text_lab.config(text=value[0])
                        image_lab.config(image=photo_image)

虽然找到了搜索字词并显示了翻译并显示了相应的图片,但我也收到了错误消息:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 1533, in __call__
    return self.func(*args)
  File "/Users/admin/Desktop/nautical_glossary/nautical_dict.py", line 186, in <lambda>
    search_btn = ttk.Button(actions_frame, text = 'SEARCH TERM', command=lambda: get_matches() & choose_image(photo_image, label_text, label_image))
TypeError: unsupported operand type(s) for &: 'NoneType' and 'NoneType'

字典的格式为key:value(例如&#39; aft&#39;:&#39; verso la poppa&#39;),图片字典的格式为key :(标题,图片地址)例如,&#39; aft&#39;:(&#39; verso la poppa&#39;,&#39; ./ images / aft.gif&#39;), 我不知道为什么我会收到错误消息以及我应该怎么做。建议,请

2 个答案:

答案 0 :(得分:2)

&安培;可能意味着不同的东西,如二进制和/或设置交集,但不像在shell中。

丑陋的黑客:替换:

get_matches() & choose_image(photo_image, label_text, label_image)

使用:

(get_matches() , choose_image(photo_image, label_text, label_image))

最佳方式:创建一个功能:

def search(photo_image, label_text, label_image):
    get_matches()
    choose_image(photo_image, label_text, label_image)

然后致电:

command=lambda: search(photo_image, label_text, label_image)

答案 1 :(得分:0)

IMO,最好的解决方案是专门为按钮创建一个函数,而不是试图将所有东西都压缩成lambda。这就是command选项的设计方式。

这将使您的代码更易于理解和维护。 lambda实际上只应用于特殊情况,而不是从按钮调用函数的常规方法。

search_btn = ttk.Button(actions_frame, text = 'SEARCH TERM', command=on_search)

def on_search():
    get_matches()
    choose_image(photo_image, label_text, label_image)

不清楚photo_imagelabel_textlabel_image来自何处?(它们是全球性的吗?)因此您可能需要进行一些调整。关键是,如果您不尝试将所有内容塞入lambda语句中,这些类型的问题更容易解决。