简体   繁体   中英

python tkinter canvas lift method does not work

I wonder how to lift and lower a canvas object in Python Tkinter. I tried canvas.lower() but it's resulting in an error prompting

_tkinter.TclError:wrong # args: should be ".!canvas lower tag0rld "belowThis?

my script:

import tkinter as tk
import PIL.ImageTk as itk
window=tk.Tk()
image1=itk.PhotoImage(file="a.png")
canvas1 = tk.Canvas(window)
a=canvas1.create_image(0,0,image=image1)
canvas1.place(x=100,y=100)
canvas1.lower()
image2 = itk.PhotoImage(file="b.png")
canvas2 = tk.Canvas(window)
b = canvas2.create_image(0,0,image=image2)
canvas2.place(x=100,y=130)
window.mainloop()

the image is just a black square and a white square.

Lowering the whole canvas:

import tkinter as tk

import tkinter as tk

window = tk.Tk()
canvas1 = tk.Canvas(window, bg="red")
canvas1.place(x=100,y=100)

canvas2 = tk.Canvas(window, bg="blue")
canvas2.place(x=100,y=130)

canvas2.tk.call('lower', canvas2._w, None)

window.mainloop()

root.mainloop()

This directly calls the tcl command but still works. The problem was that in the definition of tkinter.Canvas :

class Canvas(Widget, XView, YView):
    ...
    def tag_lower(self, *args):
        """Lower an item TAGORID given in ARGS
        (optional below another item)."""
        self.tk.call((self._w, 'lower') + args)
    lower = tag_lower

It overrides the Misc class's (the base class for all widgets) method .lower that lowers the widget. So I directly called what the Misc class would have called: self.tk.call('lower', self._w, belowThis)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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