I would like to give layers to tkinter images when we created them below code:
from tkinter import *
canvas_width = 300
canvas_height =300
master = Tk()
canvas = Canvas(master,
width=canvas_width,
height=canvas_height)
canvas.pack()
mylist = []
img = PhotoImage(file="./Images/screwsmall.png")
mylist.append (canvas.create_image(20,20, anchor=NW, image=img))
img_2 = PhotoImage(file="./Images/screwsmall.png")
mylist.append (canvas.create_image(50,20, anchor=NW, image=img_2))
mainloop()
As you can see two images are created, however the second image is on top of first image. How can I decide their layers by the code? In other words can I make the first image go on top of other with out creating order?
Use the tag_lower() and tag_raise() methods for the Canvas object:
canvas.tag_raise(mylist[0]) # move the first image to the top
Yes this works ty. Why it is tag_raise?
As far as I see I can give layers to different tags. .tag_raise(tagOrId, aboveThis) thats nice.
Many things in tkinter work on the concept of tags, where you can assign a label to a group of things. In this case it's a group of 1, but you still use the same command to work with them.