I'm trying to change the layering of Tkinter Canvas widgets. With most widgets you can force the widget above other widgets by using the lift method. However, if I try the same on a Canvas widget I get an error.
Error :
TypeError: tag_raise() got an unexpected keyword argument 'aboveThis'
An Example of my Problem :
import Tkinter as Tk
root = Tk.Tk()
w, h = 200, 200
a = Tk.Canvas(root, bg='red', width=w, height=h)
a.grid(column=0, row=0)
b = Tk.Canvas(root, bg='blue', width=w, height=h)
b.grid(co开发者_高级运维lumn=0, row=0)
a.lift(aboveThis=None)
root.mainloop()
If I do the same thing with Frame widgets, it works.
Example:
import Tkinter as Tk
root = Tk.Tk()
w, h = 200, 200
a = Tk.Frame(root, bg='red', width=w, height=h)
a.grid(column=0, row=0)
b = Tk.Frame(root, bg='blue', width=w, height=h)
b.grid(column=0, row=0)
a.lift(aboveThis=None)
root.mainloop()
The canvas lift()
method is an alias for tag_raise()
, which is used to raise not the canvas itself but entities within the canvas.
I found this comment within the Tkinter.py
source code:
# lower, tkraise/lift hide Misc.lower, Misc.tkraise/lift,
# so the preferred name for them is tag_lower, tag_raise
# (similar to tag_bind, and similar to the Text widget);
# unfortunately can't delete the old ones yet (maybe in 1.6)
If you replace a.lift(aboveThis=None)
with Tk.Misc.lift(a, aboveThis=None)
then the canvas widget is raised correctly.
I came to this question because I was actually wanting to implement the equivalent of a tk statement like
canvas-pathName raise tagOrId ?aboveThis?
in order to raise individual canvas items to a particular z-position. For those interested in the same thing, I'll just post my realization (after a little head-scratching) that this can be done in Python quite easily:
canvasObject.tag_raise(tagOrId, tagOrId2)
The second argument here just gets incorporated into the tk command line, and is then interpreted as an "aboveThis" value.
精彩评论