1
我有一个Python 3 Tkinter程序,该程序应该呈现特殊文件格式的图像。Python 3 PhotoImage在不止一次呈现时跨标签重复
from tkinter import *
image = []
path = Tk()
display = Toplevel()
display.wm_title("RCSVvisual")
display.wm_iconbitmap("favicon.ico")
path.wm_title("RCSVpath")
path.wm_iconbitmap("favicon.ico")
entry1 = Entry(path)
entry1.pack(side=LEFT)
photo = PhotoImage(width=240, height=180)
def refresh():
file_path = entry1.get()
with open(file_path, "r") as file:
for line in file:
thisLine = line.strip()
thisArray = thisLine.split(",")
image.append(thisArray)
for c1 in range(0, len(image)):
for c2 in range(0, len(image[c1])):
photo.put(image[c1][c2], (c1, c2))
button = Button(path, text="Open File", command=refresh)
button.pack(side=LEFT)
label = Label(display, image=photo)
label.pack()
path.mainloop()
display.mainloop()
这是我测试出来的文件:
#F00,#0F0,#00F,#F00
#0F0,#00F,#F00,#0F0
#00F,#F00,#0F0,#00F
#F00,#0F0,#00F,#F00
但是,看来,如果你运行该程序不止一次,图像不会取代旧的;而是显示在原始图像的右侧。我在这里做错了什么?我应该再次打包()目标标签吗?