#!/usr/bin/env python3

from tkinter import *
from tkinter import ttk

PHI = (1 + 5**0.5) / 2
canvas_height = 300

# A list of points the user placed on the canvas
points = list()
last_line = None

def add_point(event):
    """Adds a point to the 'points' list and draw it on the canvas"""
    global last_line

    points.append((event.x, event.y))
    canvas.create_oval((event.x - 2, event.y - 2, event.x + 2, event.y + 2))

    if len(points) > 1:
        point1 = points[-1]
        point2 = points[-2]
        canvas.create_line((point1[0], point1[1], point2[0], point2[1]))
    if len(points) >= 3:
        point1 = points[-1]
        point2 = points[0]
        if last_line != None:
            canvas.delete(last_line)
        last_line = canvas.create_line((point1[0], point1[1], point2[0], point2[1]))
    print(event.x, event.y)

def squarify_polygon():
    pass

root = Tk()
frame = ttk.Frame(root)
canvas = Canvas(frame, width=canvas_height*PHI, height=canvas_height)
canvas.bind('<Button-1>', add_point)
squarify = ttk.Button(frame, text='Squarify polygon', command = squarify_polygon)
frame.grid()
canvas.grid()
squarify.grid()
root.mainloop()


@@ -305,16 +305,39 @@
 t_line%0A%0A
+    # Create the point%0A
     poin
@@ -369,16 +369,20 @@
 .y))%0A   
+ i =
  canvas.
@@ -447,16 +447,90 @@
 y + 2))%0A
+    canvas.addtag('point', 'withtag', i)%0A%0A    # Connect lines to the point
 %0A    if 
@@ -605,24 +605,28 @@
 %5B-2%5D%0A       
+ i =
  canvas.crea
@@ -675,24 +675,69 @@
 point2%5B1%5D))%0A
+        canvas.addtag('line', 'withtag',  i)%0A
     if len(p
@@ -964,31 +964,302 @@
     
-print(event.x, event.y)
+    canvas.addtag('line', 'withtag', last_line)%0A    print(event.x, event.y)%0A%0Adef clear_canvas():%0A    %22%22%22Deletes the current polygon from the canvas.%22%22%22%0A    global points%0A    # Delete all points%0A    points = list()%0A    # Clear lines and points from the canvas%0A    canvas.delete('line', 'point')%0A
 %0A%0Ade
@@ -1515,16 +1515,80 @@
 olygon)%0A
+clear = ttk.Button(frame, text='Clear', command = clear_canvas)%0A
 frame.gr
@@ -1622,16 +1622,29 @@
 .grid()%0A
+clear.grid()%0A
 root.mai

Add clear button to delete polygon from canvas