[英]python tkinter canvas lift method does not work
I wonder how to lift and lower a canvas object in Python Tkinter.我想知道如何在 Python Z6F8BBEA5A81184EBA08B78919FDZ1 中提升和降低 canvas object。 I tried canvas.lower()
but it's resulting in an error prompting我试过canvas.lower()
但它导致错误提示
_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:降低整个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.这直接调用tcl
命令但仍然有效。 The problem was that in the definition of tkinter.Canvas
:问题是在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.它覆盖了降低小部件的Misc
类(所有小部件的基本 class)方法.lower
。 So I directly called what the Misc
class would have called: self.tk.call('lower', self._w, belowThis)
所以我直接调用了Misc
class 会调用的内容: self.tk.call('lower', self._w, belowThis)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.