Jesper Petersen
Jesper Petersen

Reputation: 77

Draw line on image in tkinter

I'm trying to make a script that will draw lines on an image in a python GUI. I've been able to get the image on the GUI, but do not know how to draw the additional lines. The script should be able to loop so I can draw more lines.

What I have so far:

import tkinter as Tk

root = Tk.Tk()
background_image=Tk.PhotoImage(file="map.png")
background_label = Tk.Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
root.wm_geometry("794x370")
root.title('Map')
root.mainloop()

Upvotes: 2

Views: 8062

Answers (1)

nwk
nwk

Reputation: 4050

You can do that by first placing your image on a canvas:

import tkinter as Tk

root = Tk.Tk()
canvas = Tk.Canvas(root)
background_image=Tk.PhotoImage(file="map.png")
canvas.pack(fill=Tk.BOTH, expand=1) # Stretch canvas to root window size.
image = canvas.create_image(0, 0, anchor=Tk.NW, image=background_image)
line = canvas.create_line(10, 10, 100, 35, fill="red")
root.wm_geometry("794x370")
root.title('Map')
root.mainloop()

Upvotes: 4

Related Questions