I'm trying to show picture using Tkinter, Python
It's working well in plain code, but is not in function
Code:
import time
from tkinter import *
from PIL import Image, ImageTk
def test():
global imageTk # fixes the problem
size = (100, 100)
picture = Image.open("C:\download\picture.jpg")
picture = picture.resize(size)
imageTk = ImageTk.PhotoImage(picture)
canvas.create_image(60, 60, anchor=CENTER, image=imageTk)
tk = Tk()
canvas = Canvas(tk, width=1000, height=1000)
canvas.pack()
# doesn't show image
test()
tk.mainloop()
Why code in test() function is not working, but the same code in main part is?
P.S. I checked this question: Why does Tkinter image not show up if created in a function?
Adding global to image variable to save it from GC is a solution