Long story short: I want to draw an image in a tkinter.Canvas
on a button click. Obviously, this will happen after calling mainloop()
. However, create_image()
function is not updating the canvas.
Long Story:
I'm trying to draw several images in a tkinter.Canvas
on a button click. This is roughly my code.
from PIL import Image
from PIL.ImageTk import PhotoImage
# this code is inside an event handler of a button click
# this is also a class-method
ROW_COUNT = 1
COL_COUNT = 3
for i in range(ROW_COUNT):
for j in range(COL_COUNT):
raw_img = Image.open(img_list[i * COL_COUNT + j])
resized_img = raw_img.resize((160, 160), Image.ANTIALIAS)
img = PhotoImage(resized_img)
x = j * 170
y = i * 170
self.canvas.create_image(x, y, anchor=NW, image=img)
self.canvas.update_idletasks()
As this code executes on a button click, this will surely be after calling mainloop()
. However, nothing happens after the execution of this code. If I recall mainloop()
after the loops, only the last image gets drawn. If I call mainloop()
in the inner loop, only the first image gets drawn (and probably both the loops breaks).
How can I draw all the images?