You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python tkinter如何调整Canvas内控件与图片的堆叠显示顺序

问题根源

Tkinter Canvas存在固定层级规则:所有通过create_image、create_rectangle等方法绘制的内部图元,层级永远低于通过create_window嵌入的原生控件(Entry、Button等),所以即使后创建image2,也无法覆盖先创建的Entry控件。

解决方案

将image2封装为Label原生控件,再通过create_window嵌入Canvas,二者同为嵌入窗口控件即可调整堆叠顺序,修改后代码如下:

from tkinter import *
# 类内部代码
self.img_main = PhotoImage(file=r"C:\Bart\My library\Python\Grinding\GUI\capture\image1.png")
self.canvas_main = Canvas(self, width=1366, height=768, bd=0)
self.canvas_main.pack(fill=BOTH, expand=True)
self.canvas_main.create_image(0,0, image=self.img_main, anchor=NW)

self.entry = Entry(self, width=12, justify="center", bg="#A5C1DE")
self.entry_win = self.canvas_main.create_window(72, 287, height=32, anchor=NW, window=self.entry)

# 封装image2为Label控件
self.cbb_pro_sel_img_1 = PhotoImage(file=r"C:\Bart\My library\Python\Grinding\GUI\capture\image2.png")
self.img2_label = Label(self, image=self.cbb_pro_sel_img_1, bd=0)
self.img2_win = self.canvas_main.create_window(36, 75, anchor=NW, window=self.img2_label)

# 调整堆叠顺序实现 image2 > Entry > 背景图
self.canvas_main.tag_raise(self.img2_win, self.entry_win)

如果后续需要调整为Entry在image2上方,执行self.canvas_main.tag_raise(self.entry_win, self.img2_win)即可。

内容的提问来源于stack exchange,提问作者racoonisracoonracoon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 21:15:03