【问题标题】:Tkinter Not Showing image in Label [duplicate]Tkinter未在标签中显示图像[重复]
【发布时间】:2022-01-10 13:42:08
【问题描述】:

这是我的代码:

import tkinter as tk
win=tk.Tk()
class loadfileclass():
    filenamevar=''
    data=''
    try:
        filenamevar=tk.StringVar()
        fname=filenamevar.get()
        filenamevar.set("")
        mainfile=open(str(fname),"rb")
        data=mainfile.read()
        data=data.split("##########")
    except:
        pass

tk.Label(text='Filename: ').pack()
tk.Entry(textvariable=loadfileclass.filenamevar).pack()
tk.Button(text='Load',command=loadfileclass).pack()
data=loadfileclass.data
for i in data:
    def fun():
        global photo,data
        data.replace("##########","\n")
        fd=data.split("==========")
        photo=Tkinter.PhotoImage(file=fd[1])
        tk.Label(fd[0],image=photo).pack()
    fun()
win.mainloop()

标签不会显示,甚至图像或文本也不会显示。

好的,这是我在输入中输入名称的文件的文件内容:

图片

==========
(opened image in notepad and copy pasted it here)
##########
IMAGE
==========
(opened image in notepad and copy pasted it here again)
##########

如何解决?

【问题讨论】:

  • 如果您认为该问题不重复,请解释为什么重复问题中的答案不适用于您的问题。不要只添加短语“不重复”,因为这不会让您的问题重新打开。

标签: python image tkinter tkinter-entry tkinter-label


【解决方案1】:

经典图像参考问题。试试这样:

def fun():
    global photo,data
    data.replace("##########","\n")
    fd=data.split("==========")
    photo=tk.PhotoImage(file=fd[1])
    lbl = tk.Label(fd[0],image=photo)
    lbl.pack()
    lbl.img_ref = photo # keep the reference

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多