2017-06-13 140 views
2

我创建了多个画布,并且它们重叠。我想把一个特定的画布带到前面。在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 

回答

2

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

要更改窗口小部件级别上的堆叠顺序,您应该使用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 
+0

奇妙的将我引入到tk.Misc类。对于示例代码中的错误感到抱歉。我只是想用它们来展示这个想法,但它本可以更好。 – ffrree

1

您遇到的问题是因为您选择使用Canvas。画布有一个lift方法覆盖默认的lift函数。画布的lift方法用于提升画布上绘制的东西,而不是画布本身。如果您选择使用框架而不是画布,您的代码就可以工作。

可以使用lift方法是使用画布的情况下,其它库的一部分:

tk.Misc.lift(a)