Python Programming: An Introduction to Computer Science

(Nora) #1
62 CHAPTER5. OBJECTSANDGRAPHICS

5.2 GraphicsProgramming


Moderncomputerapplicationsarenotlimitedtothesortoftextualinputandoutputthatwehave beenusing
sofar. Mostoftheapplicationsthatyouarefamiliarwithprobablyhave a so-calledGraphicalUserInterface
(GUI)thatprovidesvisualelementslike windows,icons(representative pictures),buttonsandmenus.
Interactive graphicsprogrammingcanbeverycomplicated;entiretextbooksaredevotedtotheintricacies
ofgraphicsandgraphicalinterfaces. Industrial-strengthGUIapplicationsareusuallydevelopedusinga
dedicatedgraphicsprogrammingframework.PythoncomeswithitsownstandardGUImodulecalledTkinter.
AsGUIframeworksgo,Tkinteris oneofthesimplesttouse,andPythonis greatlanguagefordeveloping
real-worldGUIs.Evenso,takingthetimetolearnTkinterwoulddetractfromthemorefundamentaltaskof
learningtheprinciplesofprogramminganddesignthatarethefocusofthisbook.
To make learningeasier, I have writtena graphicslibrary(graphics.py) forusewiththisbook.This
libraryis freelyavailableasa Pythonmodulefile^1 andyouarewelcometouseit asyouseefit.Usingthe
libraryis aseasyasplacinga copy ofthegraphics.pyfilein thesamefolderasyourgraphicsprogram(s).
Alternatively, youcanputgraphics.pyinthesystemdirectorywhereotherPythonlibrariesarestoredso
thatit canbeusedfromany folderonthesystem.
Thegraphicslibrarymakesit easytowritesimplegraphicsprograms.Asyoudo,youwillbelearning
principlesofobject-orientedprogrammingandcomputergraphicsthatcanbeappliedinmoresophisticated
graphicalprogrammingenvironments.Thedetailsofthegraphicsmodulewillbeexploredinlatersec-
tions.Herewe’ll concentrateona basichands-onintroductiontowhetyourappetite.
Asusual,thebestwaytostartlearningnew conceptsis torollupyoursleevesandtryoutsomeexamples.
Thefirststepis toimportthegraphicsmodule.Assumingyouhave placedgraphics.pyin anappropriate
place,youcanimportthegraphicscommandsintoaninteractive Pythonsession.





import graphics





Nextweneedtocreatea placeonthescreenwherethegraphicswillappear. Thatplaceis agraphics
windoworGraphWin, whichis providedbythegraphicsmodule.





win = graphics.GraphWin()





Thiscommandcreatesa new window onthescreen.Thewindow willhave thetitle“GraphicsWindow.” The
GraphWinmayoverlapyourPythoninterpreterwindow, soyoumighthave toresizethePythonwindow to
make bothfullyvisible.Figure5.1showsanexamplescreenview.
TheGraphWinis anobject,andwehave assignedit tothethevariablecalledwin. We canmanipulate
thewindowobjectthroughthisvariable,similartothewaythatfileobjectsaremanipulatedthroughfile
variables.Forexample,whenwearefinishedwitha window, wecandestroy it.Thisis donebyissuingthe
closecommand.





win.close()





Typingthiscommandcausesthewindow tovanishfromthescreen.
We willbeworkingwithquitea few commandsfromthegraphicslibrary, andit getstedioushaving
totypethegraphics.notationeverytimeweuseone.Pythonhasanalternative formofimportthatcan
helpout.


from graphics import*


Thefromstatementallowsyoutoloadspecificdefinitionsfroma librarymodule. Youcaneitherlistthe
namesofdefinitionstobeimportedoruseanasterisk,asshown,toimporteverythingdefinedinthemodule.
Theimportedcommandsbecomedirectlyavailablewithouthavingtoprefacethemwiththemodulename.
Afterdoingthisimport,wecancreateaGraphWinmoresimply.


win = GraphWin()


(^1) SeeAppendixA forinformationonhow toobtainthegraphicslibraryandothersupportingmaterialsforthisbook.

Free download pdf