76 CHAPTER5. OBJECTSANDGRAPHICS
win.setCoords(0.0,0.0, 10.0, 10.0)
message = Text(Point(5,0.5), "Click on three points")
message.draw(win)
# Get and drawthree vertices of triangle
p1 = win.getMouse()
p1.draw(win)
p2 = win.getMouse()
p2.draw(win)
p3 = win.getMouse()
p3.draw(win)
# Use Polygon objectto draw the triangle
triangle = Polygon(p1,p2,p3)
triangle.setFill("peachpuff")
triangle.setOutline("cyan")
triangle.draw(win)
# Wait for anotherclick to exit
message.setText("Clickanywhere to quit.")
win.getMouse()
main()
Thethree-clicktriangleillustratesa couplenew featuresofthegraphicsmodule.Thereis notriangle
class;howeverthereis a generalclassPolygonthatcanbeusedforany multi-sided,closedshape.The
constructorforPolygonacceptsany numberofpointsandcreatesa polygonbyusinglinesegmentsto
connectthepointsintheordergivenandtoconnectthelastpointbacktothefirst. Atriangleisjusta
three-sidedpolygon.Oncewehave threePointsp1,p2, andp3, creatingthetriangleis a snap.
triangle = Polygon(p1,p2, p3)
Youshouldalsostudyhow theTextobjectis usedtoprovideprompts.A singleTextobjectis created
anddrawnnearthebeginningoftheprogram.
message = Text(Point(5,0.5), "Click on three points")
message.draw(win)
To changetheprompt,wedon’t needtocreatea newTextobject,wecanjustchangethetextthatis
displayed.Thisis doneneartheendoftheprogramwiththesetTextmethod.
message.setText("Clickanywhere to quit.")
Asyoucansee,thegetMousemethodofGraphWinprovidesa simplewayofinteractingwiththeuser
ina graphics-orientedprogram.
5.6.2 HandlingTextualInput.
Inthetriangleexample,alloftheinputwasprovidedthroughmouseclicks.Thegraphicsmodulealso
includesa simpleEntryobjectthatcanbeusedtogetkeyboardinputinaGraphWin.
AnEntryobjectdrawsa boxonthescreenthatcancontaintext.It understandssetTextandgetText
methodsjustlike theTextobjectdoes.Thedifferenceis thatthecontentsofanEntrycanbeeditedbythe
user. Here’s a versionofthetemperatureconversionprogramfromChapter2 witha graphicaluserinterface: