在 Tkinter 中将画布提升到其他画布之上

ffr*_*ree 4 python canvas tkinter

我创建了许多画布,它们重叠。我想把一块特定的画布放在前面。

我似乎没有找到办法做到这一点。提升方法似乎不起作用,例如

import tkinter as Tk
w=tk.Tk()
a=tk.Canvas(w,width=20, height=30)
a.place(x=20, y=30)
b=tk.Canvas(w,width=20, height=30)
b.place(x=25, y=35)
w.lift(b)             # try to bring b to the front, but nothing happens
Run Code Online (Sandbox Code Playgroud)

Laf*_*los 5

你的画布就在那里,问题是,它们的颜色与窗口的其余部分相同。您可以添加背景颜色来区分它们。

要更改小部件级别的堆叠顺序,您应该使用Tkinter.Misc类。

import tkinter as tk #fixed typo in here
w=tk.Tk()
a=tk.Canvas(w,width=20, height=30, bg="red")
a.place(x=20, y=30)
b=tk.Canvas(w,width=20, height=30, bg="blue")
b.place(x=25, y=35)
tk.Misc.lift(a)
w.mainloop() #even if some IDEs adds mainloop, it's always better to add it explicitly
Run Code Online (Sandbox Code Playgroud)