简体   繁体   English

在Tkinter的其他画布上提升画布

[英]Lift canvas on top of other canvas(s) in Tkinter

I have created number of canvases, and they overlap. 我已创建了一些画布,并且它们重叠。 I would like to bring a particular canvas to the front. 我想把一块特殊的帆布带到前面。

I don't seem to find a way to do it. 我似乎没有办法做到这一点。 The lift method does not seem to work, eg 电梯方法似乎不起作用,例如

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

Your canvases are there, the problem is, their color is the same as the rest of the window. 您的画布在那里,问题是,它们的颜色与窗口的其余部分相同。 You can add background colors to differentiate them. 您可以添加背景颜色以区分它们。

To change stacking orders on widget level, you should use Tkinter.Misc class. 要在窗口小部件级别更改堆叠顺序,您应该使用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

The problem you are having is because you are choosing to use a Canvas . 您遇到的问题是因为您选择使用Canvas Canvases have a lift method that overrides the default lift function. 画布具有覆盖默认lift功能的lift方法。 The lift method of the canvas is for lifting something drawn on the canvas rather than the canvas itself. 画布的lift方法用于提升画布上绘制的东西而不是画布本身。 If you had chosen to use a frame rather than a canvas, your code would have worked. 如果你选择使用框架而不是画布,那么你的代码就可以了。

You can use the lift method that is part of the Misc library in the case of using a canvas: 在使用画布的情况下,您可以使用属于Misc库的lift方法:

tk.Misc.lift(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM