Reputation:
I have a Python 3 Tkinter program that is supposed to render an image from a special file format.
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()
And this is the file I test it out on:
#F00,#0F0,#00F,#F00
#0F0,#00F,#F00,#0F0
#00F,#F00,#0F0,#00F
#F00,#0F0,#00F,#F00
However, it appears that if you run the program more than once, the image does not replace the old one; instead displaying to the right of the original image. Am I doing something wrong here? Should I .pack() the target label again?
Upvotes: 1
Views: 63
Reputation: 7735
You are always appending to image
.
If you want to create new image, you should empty existing list first.
def refresh():
image = []
file_path = entry1.get()
....
Also, almost always you'd need only one mainloop in your program. You should remove display.mainloop()
line.
Upvotes: 1