Warm tip: This article is reproduced from serverfault.com, please click

Python tkinter create image with different layers?

发布于 2020-11-29 16:14:43

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?

enter image description here

Questioner
Meric Ozcan
Viewed
0
Novel 2020-11-30 00:17:23

Use the tag_lower() and tag_raise() methods for the Canvas object:

canvas.tag_raise(mylist[0]) # move the first image to the top