I want to create just one straight line and also add some button to reset it, and from there, create another one.
Currently my code creates one straight lime with the mouse, but if after I finished the first line I click it creates another line.
import tkinter as tk
from PIL import ImageTk
root = tk.Tk()
canvas = tk.Canvas(width = 600, height = 400)
canvas.pack(expand = tk.YES, fill = tk.BOTH)
coords = {"x":0,"y":0,"x2":0,"y2":0}
# keep a reference to all lines by keeping them in a list
lines = []
def click(e):
# define start point for line
coords["x"] = e.x
coords["y"] = e.y
# create a line on this point and store it in the list
l = lines.append(canvas.create_line(coords["x"],coords["y"],coords["x"],coords["y"], width=5, fill='red'))
def drag(e):
# update the coordinates from the event
coords["x2"] = e.x
coords["y2"] = e.y
print(e.x, e.y)
# Change the coordinates of the last created line to the new coordinates
l = canvas.coords(lines[-1], coords["x"],coords["y"],coords["x2"],coords["y2"])
canvas.itemconfigure(l, fill="black")
canvas.bind("<ButtonPress-1>", click)
canvas.bind("<B1-Motion>", drag)
print(coords["x"],coords["y"],coords["x2"],coords["y2"])
root.mainloop()