python tkinter 无法在 canvas 上实现 lift 方法
python tkinter cannot implement lift method on canvas
我正在尝试在 Python Tkinter 中提升和降低 canvas 对象。我试过 canvas.lower()
但它导致错误提示
_tkinter.TclError:wrong # args: should be ".!canvas lower tag0rld "belowThis?
我的脚本:
import tkinter as tk
import PIL.ImageTk as itk
window=tk.Tk()
image1=itk.PhotoImage(file=“image_1.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=“image_2.png")
canvas2 = tk.Canvas(window)
b = canvas2.create_image(0,0,image=image2)
canvas2.place(x=100,y=130)
window.mainloop()
图像只是一个黑色方块和一个白色方块,所以它们无关紧要。
整体降低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()
这直接调用了 tcl
命令,但仍然有效。问题是在 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
它覆盖了降低小部件的 Misc
class(所有小部件的基础 class)方法 .lower
。所以我直接调用了 Misc
class 会调用的内容:self.tk.call('lower', self._w, belowThis)
我正在尝试在 Python Tkinter 中提升和降低 canvas 对象。我试过 canvas.lower()
但它导致错误提示
_tkinter.TclError:wrong # args: should be ".!canvas lower tag0rld "belowThis?
我的脚本:
import tkinter as tk
import PIL.ImageTk as itk
window=tk.Tk()
image1=itk.PhotoImage(file=“image_1.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=“image_2.png")
canvas2 = tk.Canvas(window)
b = canvas2.create_image(0,0,image=image2)
canvas2.place(x=100,y=130)
window.mainloop()
图像只是一个黑色方块和一个白色方块,所以它们无关紧要。
整体降低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()
这直接调用了 tcl
命令,但仍然有效。问题是在 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
它覆盖了降低小部件的 Misc
class(所有小部件的基础 class)方法 .lower
。所以我直接调用了 Misc
class 会调用的内容:self.tk.call('lower', self._w, belowThis)