2016-07-22 540 views
1

我想顯示圖像並在其上繪製座標系(x-ax,y-ax)。 我可以顯示圖像如何使用tkinter在圖像上繪製座標系?

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-AX/Y-AX線

(如果你知道一種方法來繪製一個閃存,而不是一條線的話會更好)
canvas = Canvas(root) 
canvas.create_line(x0,y0,x1,y1) 
canvas.pack() 

我不能做的是將此行放在圖像的頂部。我嘗試使用canvas.place(),但是當我將它放在圖像的頂部時,我無法再看到圖像。有沒有辦法讓畫布變得透明?或者還有什麼我可以做的嗎?我是Tkinter的新手。

謝謝。

編輯:顯然這是不可能做出的畫布透明Transparent canvas in tkinter python

但我可以在畫布添加一個背景圖像,然後畫線,讓我可以看到兩者兼而有之?

+1

你的代碼有一些錯誤。 –

回答

3

你必須將圖像放置在畫布上,然後把軸線在它的上面:

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

噹噹!

screenshot of tkinter window displaying image and axes