Tkinter: Difference between revisions

Created page with "Tkinter is a Python API for the Tk GUI. It is built into the Python standard library and is cross platform. ==Images== To display an image: <syntaxhighlight lang="python"> im..."
 
Line 19: Line 19:
photo_img = ImageTk.PhotoImage(image=image)
photo_img = ImageTk.PhotoImage(image=image)
canvas_image = canvas.create_image(0, 0, image=photo_img, anchor=tk.NW)
canvas_image = canvas.create_image(0, 0, image=photo_img, anchor=tk.NW)
window.mainloop()


# To update the image
# To update the image later on..
image_url2 = 'https://via.placeholder.com/256/FF00FF/'
image_url2 = 'https://via.placeholder.com/256/FF00FF/'
image2 = Image.open(requests.get(image_url, stream=True).raw)
image2 = Image.open(requests.get(image_url, stream=True).raw)
photo_img = ImageTk.PhotoImage(image=image2)
photo_img = ImageTk.PhotoImage(image=image2)
canvas.itemconfigure(canvas_image, image=photo_img)
canvas.itemconfigure(canvas_image, image=photo_img)
window.mainloop()
</syntaxhighlight>
</syntaxhighlight>


;Notes
;Notes
* Make sure the <code>ImageIk.PhotoImage</code> does not get garbage collected.
* Make sure the <code>ImageIk.PhotoImage</code> does not get garbage collected.