It seems to be a very common application, but I could not make it work:
def Img2Canvas(Img,Canv): # this function will put image on a canvas by stretching it
Canv.update()
H=Canv.winfo_height()
W=Canv.winfo_width()
print([W,H])
temp=ImageTk.PhotoImage(Img.resize((W,H)))
Canv.create_image(1,1,anchor=tk.NW,image=temp)
Then I called this function in main program:
cv1=tk.Canvas(root,width=200,height=200,bg='yellow')
Img2Canvas(p1.Img,cv1)
1) this does not work, The canvas is not updated, and I just got a Yellow background. It only works if I do not do temp=ImageTk.PhotoImage(Img.resize((W,H)))
inside the function, but resize the image outside of function and input temp directly...
2) the W
and H
seems to be 204 instead of 200, so is winfo_height()
always give you 4 more pixels?
3) is there a better way to display a figure file (jpg, png, etc.) in Tkinter?