As the title says, I need to get the image of a canvas
To get the image of a Label, I simply type
JavaScript
x
3
1
aLable = Label(root,image = AnImage)
2
aLabel.cget("image")
3
I can check to see if AmImage
is in aLabel
using cget()
so how do I do the same with the Canvas?
cget("image")
doesn’t seem to work
Advertisement
Answer
I Found a way to do it, So I will try to explain it
JavaScript
1
9
1
from tkinter import *
2
from PIL import ImageTk,Image
3
4
root = Tk()
5
An_Image = ImageTk.PhotoImage(Image.open('flower.jpg'))
6
Test = Canvas(root, width = 1000, height = 1000)
7
Test.create_image(25,25, image=An_Image, anchor=CENTER, tag = "abc")
8
Test.place(x=10,y = 10)
9
A Tag is a name to certain parts of a widget like the Canvas.
"abc"
is the name of the tag of the image that got created, you can name it however you want, the tag cannot contain spaces and it must be a string tag = "your_string"
.
The reason we need to add a tag to the item is this
JavaScript
1
7
1
Test.itemcget(Tag_id, Item) # Tag_id is the Item tag
2
3
#example
4
Test.itemcget("abc", "image") # this is how I get the image from the Test Canvas
5
6
7