Python Programming: An Introduction to Computer Science

(Nora) #1
5.7.GRAPHICSMODULEREFERENCE 79

5.7 GraphicsModuleReference


Theexamplesinthischapterhave touchedonmostoftheelementsinthegraphicsmodule.Thissection
providesa completereferencetotheobjectsandfunctionsprovidedinthegraphicslibrary. Experienced
programmersusethesesortsofguidestolearnaboutnew libraries.Youwillprobablywanttoreferbackto
thissectionoftenwhenyouarewritingyourowngraphicalprograms.


5.7.1 GraphWinObjects


AGraphWinobjectrepresentsa window onthescreenwheregraphicalimagesmaybedrawn.A program
maydefineany numberofGraphWins. AGraphWinunderstandsthefollowingmethods.


GraphWin(title, width,height)Constructsa new graphicswindow fordrawingonthescreen.
Theparametersareoptional,thedefaulttitleis “GraphicsWindow,” andthedefaultsizeis 200x 200.


plot(x, y, color)Drawsthepixel at



xy inthewindow. Coloris optional,blackis thedefault.

plotPixel(x, y, Color)Drawsthepixel at the“raw”position



xy ignoringany coordinatetrans-
formationssetupbysetCoords.

setBackground(color)Setsthewindowbackgroundtothegivencolor. Theinitialbackgroundis
gray. Seesection5.7.5forinformationonspecifyingcolors.


close()Closestheon-screenwindow.


getMouse()Pausesfortheusertoclicka mouseinthewindow andreturnswherethemousewasclicked
asaPointobject.


setCoords(xll, yll,xur, yur)Setsthecoordinatesystemofthewindow. Thelowerleftcorner
is



xllyll andtheupperrightcorneris


xuryur. Allsubsequentdrawingwillbedonewithrespect
tothealteredcoordinatesystem(exceptforplotPixel).

flush()Updatestheappearanceofthewindowtoreflectalldrawingoperationsthathave beendoneso
far. Innormaloperation,thewindowis onlyupdatedduring“idle”periods.Asequenceofdrawing
commandsmayendupappearingallat once.If youwanttogetananimationeffect,flushshouldbe
calledat appropriateplacesintheprogramtoperformdrawingoperationsincrementally.


5.7.2 GraphicsObjects


Themoduleprovidesthefollowingclassesofdrawableobjects:Point,Line,Circle,Oval,Rectangle,
Polygon, andText. Allobjectsareinitiallycreatedunfilledwitha blackoutline.Allgraphicsobjectssup-
portthefollowinggenericsetofmethods.


setFill(color)Setstheinterioroftheobjecttothegivencolor.


setOutline(color)Setstheoutlineoftheobjecttothegivencolor.


setWidth(pixels)Setsthewidthoftheoutlineoftheobjecttothismany pixels.


draw(aGraphWin)DrawstheobjectintothegivenGraphWin.


undraw()Undrawstheobjectfroma graphicswindow. Thisproducesanerrorif theobjectis notcurrently
drawn.


move(dx,dy)Movestheobjectdxunitsinthexdirectionanddyunitsintheydirection.If theobjectis
currentlydrawn,theimageis adjustedtothenew position.


clone()Returnsa duplicateoftheobject.Clonesarealwayscreatedinanundrawnstate.Otherthanthat,
they areidenticaltotheclonedobject.

Free download pdf