【问题标题】:How to draw a coordinate system on top of an image using tkinter?如何使用 tkinter 在图像上绘制坐标系?
【发布时间】:2016-07-22 12:20:42
【问题描述】:

我想显示一个图像并在其上绘制一个坐标系(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-axe/y-axe 画一条线(如果你知道一种画闪光而不是线的方法会更好)

canvas = Canvas(root)
canvas.create_line(x0,y0,x1,y1)
canvas.pack()

我不能做的是将这条线放在图像的顶部。我尝试使用 canvas.place(),但是当我把它放在图像顶部时,我再也看不到图像了。有没有办法让画布透明?或者还有什么我可以做的吗?我是 Tkinter 的新手。

谢谢。

编辑:显然不可能使画布透明Transparent canvas in tkinter python

但是我可以在画布中添加背景图像然后画线,这样我就可以看到两者了吗?

【问题讨论】:

  • 你的代码有一些错误。

标签: python canvas tkinter


【解决方案1】:

你必须把图像放在画布上,然后把轴线放在它上面:

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

哒哒!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    相关资源
    最近更新 更多