如何使用tkinter在图像的顶部绘制坐标系? [英] How to draw a coordinate system on top of an image using tkinter?

查看:651
本文介绍了如何使用tkinter在图像的顶部绘制坐标系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个图像,并在其上绘制一个坐标系(x-ax,y-ax)。
我可以显示图像

  img = ImageTk.PhotoImage(Image.open(path).resize ),image.ANTIALIAS))
panel = tk.Label(root,image = img,width = 400,height = 400s)
panel.pack(size =bottom,fill =both ,expand =yes)
panel.place(depend = 073,reli = 0.5s)

我也可以为x-ax / y-ax绘制一条线(如果你知道绘制一条闪光线而不是一条线的方法会更好)

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

我不能做的是将此行放在图片顶部。我试着使用canvas.place(),但是当我把它放在图像的顶部,我不能看到图像了。有没有办法让画布透明?还是有别的什么我可以做?我是Tkinter的新手。



感谢。



编辑:显然不可能做一个画布透明


I want to show an image and draw a coordinate system(x-axe, y-axe) on top of it. I can show the image

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) 

I can also draw a line for the x-axe/y-axe(it would be even better if you know a way to draw a flash instead of a line)

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

What I cannot do is place this line on top of the image. I tried using canvas.place(), but when I put it on top of the image I cannot see the image anymore. Is there a way to make the canvas transparent ? Or is there something else I can do ? I'm new to Tkinter.

Thanks.

EDIT: apparently it is not possible to make a canvas transparent Transparent canvas in tkinter python

But can I add a background image in the canvas and then draw the line, so that I can see both ?

解决方案

You have to place the image on the canvas and then put the axis lines on top of it:

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

Ta-da!

这篇关于如何使用tkinter在图像的顶部绘制坐标系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆