1
votes

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():
    size = (100, 100)
    picture = Image.open("C:\download\picture.jpg")
    picture = picture.resize(size)
    imageTk = ImageTk.PhotoImage(picture)
    car = canvas.create_image(60, 60, anchor=CENTER, image=imageTk)
    
tk = Tk()
canvas = Canvas(tk, width=1000, height=1000)
canvas.pack()
tk.update()


# doesn't show image
test()

# shows image
size = (100, 100)
picture = Image.open("C:\download\picture.jpg")
picture = picture.resize(size)
imageTk = ImageTk.PhotoImage(picture)
car = canvas.create_image(600, 600, anchor=CENTER, image=imageTk)

tk.mainloop()

Why code in test() function is not working, but the same code in main part is?