Tkinter 不在标签中显示图像
Tkinter Not Showing image in Label
这是我的代码:
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)
##########
如何解决?
经典图片引用问题。像这样尝试:
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
这是我的代码:
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)
##########
如何解决?
经典图片引用问题。像这样尝试:
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