我想显示一个图像并在其顶部绘制一个坐标系(x轴,y轴)。 我可以显示图像
img = ImageTk.PhotoImage(Image.open(path).resize((400,400),Image.ANTIALIAS))
panel = tk.Label(root,image = img, width = 400, height = 400s)
panel.pack(size= "bottom", fill = "both", expand = "yes")
panel.place(rely = 073, rely = 0.5s)
我还可以为x轴/y轴画一条线(如果你知道如何绘制闪光而不是线,那就更好了)
canvas = Canvas(root)
canvas.create_line(x0,y0,x1,y1)
canvas.pack()
我不能做的是将此线放在图像顶部。我尝试使用canvas.place(),但是当我将它放在图像之上时,我再也看不到图像了。有没有办法让 Canvas 透明?或者还有什么我可以做的吗?我是 Tkinter 的新手。
谢谢。
编辑:显然不可能使 Canvas 透明 Transparent canvas in tkinter python
但是我可以在 Canvas 中添加背景图像然后绘制线条,以便我可以看到两者吗?
最佳答案
您必须将图像放置在 Canvas 上,然后将轴线放在其顶部:
import Tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
img_path = 'water-drop-splash.png'
img = ImageTk.PhotoImage(Image.open(img_path).resize((400,400), Image.ANTIALIAS))
canvas = tk.Canvas(root, height=400, width=400)
canvas.create_image(200, 200, image=img)
canvas.create_line(0, 200, 399, 200, dash=(2,2)) # x-axis
canvas.create_line(200, 0, 200, 399, dash=(2,2)) # y-axis
canvas.pack()
root.mainloop()
哒哒!
关于python - 如何使用 tkinter 在图像顶部绘制坐标系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38526285/