Python 3.x tkinter根据输入字段中输入的键显示图像

Python 3.x tkinter根据输入字段中输入的键显示图像,python-3.x,tkinter,Python 3.x,Tkinter,我试图根据输入到着色输入字段中的键显示不同的图像。我在函数中返回None来搜索字典,字典中包含搜索词的关键字和图像名称的值。我不知道如何纠正我的错误。我的最终目标是创建一个着色GUI,它是一个“说明性”字典,感谢任何帮助或评论 ''' 示例如何在字段中相邻放置文本和图像 ''' 从tkinter进口* def choose_image(): library = {'anchor':'admiral.gif', 'hello':'hello_tkinter_cartoon.gif'}

我试图根据输入到着色输入字段中的键显示不同的图像。我在函数中返回None来搜索字典,字典中包含搜索词的关键字和图像名称的值。我不知道如何纠正我的错误。我的最终目标是创建一个着色GUI,它是一个“说明性”字典,感谢任何帮助或评论 ''' 示例如何在字段中相邻放置文本和图像 ''' 从tkinter进口*

def choose_image():
    library = {'anchor':'admiral.gif', 'hello':'hello_tkinter_cartoon.gif'}
    term = e.get()
    for desc, image in library.items():
        if term == desc:
            piccie = image
            return desc, piccie

main = Tk()

# enter search key
e = StringVar()
term_entry = Entry( textvariable = e, bd = 2, width = 15, bg = 'mint cream', fg = 'sea green')
term_entry.grid(row =1, column =0, sticky = SE, pady = 15)

butn = Button(text = "Enter",)
butn.grid(column = 1, row = 1, sticky = W)

piccie = choose_image()
print(piccie)
piccie = PhotoImage(file=piccie)
desc = choose_image()
# two results fields adjacent

w1 = Label(main, image =piccie).grid(column = 1, row =0)
w2 = Label(main,
           text = desc,
           justify=LEFT,
           padx = 10).grid(row = 0, column = 0)

if __name__ == '__main__':
    main.mainloop()

在用户有机会输入任何内容之前,您的代码正在调用
choose_image()
。您需要等待调用该函数,直到用户输入某个内容并按下enter键、单击某个按钮或类似的内容

它返回
None
,因为这是Python中任何函数返回的默认返回值

第一次调用
choose_image()
e.get()
返回一个空字符串,因此以下代码:

piccie = image
return desc, piccie
不会执行,因为
term==desc
将不为真

例如,当
e.get()
返回空字符串时,可以通过返回默认值元组来解决此问题:

library = {'anchor':'admiral.gif', 'hello':'hello.gif'}
term = e.get()

if term == "":
    return "anchor", "admiral.gif"
请注意,在同一函数中,
piccie
变量是
if
块的局部变量,而不是全局变量引用
PhotoImage
对象。另一方面,返回一个包含图片路径的元组,该元组应足以设置
PhotoImage
对象

第二个问题是,您正在为
PhotoImage
对象的属性
file
指定一个字符串的最终元组,但它应该只需要一个表示图片路径的字符串:

piccie = choose_image()
piccie = PhotoImage(file=piccie[1])
请注意,我正在将元组的第二个元素指定给
文件
属性

我不明白你为什么第二次调用
choose_image()
,你不需要这样做

这是完整的代码,有一些改动。如果你不明白什么,就问吧

from tkinter import *


def choose_image(photo_image, text_lab, image_lab):
    library = {'anchor':'admiral.gif', 'hello': 'hello.gif'}
    term = e.get()

    if term == "":
        photo_image.config(file="admiral.gif")
        text_lab.config(text="anchor")
        image_lab.config(image=photo_image)
    else:
        for desc, image in library.items():
            if term == desc:
                photo_image.config(file=image)
                text_lab.config(text=desc)
                image_lab.config(image=photo_image)

main = Tk()

e = StringVar()
term_entry = Entry(textvariable=e, bd=2, width = 15, bg='mint cream', fg='sea green')
term_entry.grid(row=1, column=0, sticky=SE, pady=15)

label_text = Label(main)
label_text.grid(column=1, row=0)

label_image = Label(main, justify=LEFT, padx=10)
label_image.grid(row=0, column=0)

photo_image = PhotoImage(file="admiral.gif")

choose_image(photo_image, label_text, label_image)

butn = Button(text="Enter", command=lambda: choose_image(photo_image, label_text, label_image))
butn.grid(column=1, row=1, sticky=W)

main.mainloop()
另请查看本文:


非常感谢你。我理解你的逻辑。如果我想要更广泛的描述,而不是简单地使用字典中的键,我想我应该将字典的值设置为包含标题和图像的元组,然后相应地引用它们。这是正确的方法吗?谢谢你。将键的值作为包含标题和图像的元组也可以完美地工作。