rectangletangle rectangletangle - 7 days ago 5
Python Question

Tkinter Canvas Problems

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(column=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()

Answer

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.

Comments