So here's the code for a program where the user can click on a point and it draws a point and then subsequent clicks draw more lines all attached to the previous line. How would I edit this program to just let the user press down on the button and have like (xp1, yp1) and then drag some where and release at (xp2, yp2) then draw a line between (xp1, yp1) and (xp2, yp2). Finally it would let the user create many different lines then eventually be able to clear the canvas screen by pressing "c". Like I know the last thing would have to bind some function to "c" but I don't know what it is.
from Tkinter import Canvas, Tk, mainloop
import Tkinter as tk
# Image dimensions
w,h = 640,480
# Create canvas
root = Tk()
canvas = Canvas(root, width = w, height = h, bg = 'white')
canvas.pack()
# Create poly line
class PolyLine(object):
def __init__(x, canvas):
x.canvas = canvas
x.start_coords = None # first click
x.end_coords = None # subsequent clicks
def __call__(x, event):
coords = event.x, event.y # coordinates of the click
if not x.start_coords:
x.start_coords = coords
return
x.end_coords = coords # last click
x.canvas.create_line(x.start_coords[0], # first dot x
x.start_coords[1], # first dot y
x.end_coords[0], # next location x
x.end_coords[1]) # next location y
x.start_coords = x.end_coords
canvas.bind("<Button-1>", PolyLine(canvas)) # left click is used
mainloop()
Thank you so much for your time! I really appreciate it!