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

其他-Python Tkinter创建具有不同图层的图像?

(其他 - Python tkinter create image with different layers?)

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

当我们在代码下面创建tkinter图像时,我想为其赋予层:

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()

如你所见,创建了两个图像,但是第二个图像位于第一个图像的顶部。如何通过代码确定它们的层?换句话说,我可以不按创建顺序将第一个图像放在另一个图像上吗?

在此处输入图片说明

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

对Canvas对象使用tag_lower()和tag_raise()方法:

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