#polygon2.py
"""Interactively chose polygon vertices.
Illustrates use of Text, Line, Entry, Polygon, getMouse.
"""

from graphics import *

def main():
    width = 300
    height = 300
    infoHeight = 15         
    lineHeight = 2* infoHeight
    win = GraphWin("Polygon Test", width, height)
    instructions = Text(Point(width/2 - 15, infoHeight),
                     "Enter # of points, then click to continue.")
    instructions.draw(win)
    sizeEntry = Entry(Point(width - 15, infoHeight),2)
    sizeEntry.setText("3")
    sizeEntry.draw(win)
    Line(Point(0, lineHeight), Point(width, lineHeight)).draw(win)
    # note no variable is assigned to the Line - it is never referred to again
    
    win.getMouse()  # just indicate that size is set
    sizeStr = sizeEntry.getText() # expect digits, but it is a string
    size = int(sizeStr)
    instructions.setText("Click polygon vertices.")
    vertices = []
    poly = Polygon(vertices)  # must be created so it can be undrawn
    for i in range(size): 
        pt = win.getMouse()
        poly.undraw()
        vertices.append(pt) # append method adds to the list
        poly = Polygon(vertices)  
        poly.draw(win)

    poly.setFill("yellow")
    instructions.setText("Click anywhere to quit.")
    win.getMouse()
    win.close()

main()
