O !Lc@sdZddlZddlZddlZyddlZWnddlZYnXGddeZdZ dZ dZ dZ ej Zejd ZGd d ejZGd d Zidd6dd6dd6dd6dd6dd6d6d6ZGddZGdd eZGd!d"eZGd#d$eZGd%d&eZGd'd(eZGd)d*eZGd+d,eZGd-d.eZGd/d0eZGd1d2eZd3Z d4Z!e"d5kre!ndS(7uSimple object oriented graphics library The library is designed to make it very easy for novice programmers to experiment with computer graphics in an object oriented fashion. It is written by John Zelle for use with the book "Python Programming: An Introduction to Computer Science" (Franklin, Beedle & Associates). LICENSE: This is open-source software released under the terms of the GPL (http://www.gnu.org/licenses/gpl.html). PLATFORMS: The package is a wrapper around Tkinter and should run on any platform where Tkinter is available. INSTALLATION: Put this file somewhere where Python can see it. OVERVIEW: There are two kinds of objects in the library. The GraphWin class implements a window where drawing can be done and various GraphicsObjects are provided that can be drawn into a GraphWin. As a simple example, here is a complete program to draw a circle of radius 10 centered in a 100x100 window: -------------------------------------------------------------------- from graphics import * def main(): win = GraphWin("My Circle", 100, 100) c = Circle(Point(50,50), 10) c.draw(win) win.getMouse() # Pause to view result win.close() # Close window when done main() -------------------------------------------------------------------- GraphWin objects support coordinate transformation through the setCoords method and pointer-based input through getMouse. The library provides the following graphical objects: Point Line Circle Oval Rectangle Polygon Text Entry (for text-based input) Image Various attributes of graphical objects can be set such as outline-color, fill-color and line-width. Graphical objects also support moving and hiding for animation effects. The library also provides a very simple class for pixel-based image manipulation, Pixmap. A pixmap can be loaded from a file and displayed using an Image object. Both getPixel and setPixel methods are provided for manipulating the image. DOCUMENTATION: For complete documentation, see Chapter 4 of "Python Programming: An Introduction to Computer Science" by John Zelle, published by Franklin, Beedle & Associates. Also see http://mcsp.wartburg.edu/zelle/python for a quick referenceiNcBs|EeZdZdS(u3Generic error class for graphics module exceptions.N(u__name__u __module__u__doc__(u __locals__((u#/home/seanho/twu/python/graphics.pyu GraphicsErrors u GraphicsErroruObject currently drawnu Object doesn't support operationuIllegal option valueu!Graphics thread quit unexpectedlycCstjdS(N(u_rootuupdate(((u#/home/seanho/twu/python/graphics.pyuupdatescBs|EeZdZdddddZdZdZdZdZdZ d Z d Z d d Z d d Z dZdZdZdZdZdZdZdZdZdS(u8A GraphWin is a toplevel window for displaying graphics.uGraphics WindowicCstjt}|jd|jtjj||d|d||jj||j |j ddd|_ g|_ d|_d|_|jd|j||_||_||_d|_d|_d|_|j|rtjndS(NuWM_DELETE_WINDOWuwidthuheightiublacku F(utkuToplevelu_rootuprotocolucloseuCanvasu__init__umasterutitleupacku resizableu foregrounduitemsuNoneumouseXumouseYubindu_onClickuheightuwidthu autoflushu_mouseCallbackutransuFalseucloseduliftuupdate(uselfutitleuwidthuheightu autoflushumaster((u#/home/seanho/twu/python/graphics.pyu__init__s(            cCs|jrtdndS(Nuwindow is closed(uclosedu GraphicsError(uself((u#/home/seanho/twu/python/graphics.pyu __checkOpens cCs(|j|jd||jdS(u"Set background color of the windowubgN(u_GraphWin__checkOpenuconfigu_GraphWin__autoflush(uselfucolor((u#/home/seanho/twu/python/graphics.pyu setBackgrounds cCs(t|j|j|||||_dS(utSet coordinates of window to run from (x1,y1) in the lower-left corner to (x2,y2) in the upper-right corner.N(u Transformuwidthuheightutrans(uselfux1uy1ux2uy2((u#/home/seanho/twu/python/graphics.pyu setCoordsscCs1|jr dSd|_|jj|jdS(uClose the windowNT(ucloseduTrueumasterudestroyu_GraphWin__autoflush(uself((u#/home/seanho/twu/python/graphics.pyucloses    cCs|jS(N(uclosed(uself((u#/home/seanho/twu/python/graphics.pyuisClosedscCs|j S(N(uclosed(uself((u#/home/seanho/twu/python/graphics.pyuisOpenscCs|jrtjndS(N(u autoflushu_rootuupdate(uself((u#/home/seanho/twu/python/graphics.pyu __autoflushs ublackcCsP|j|j||\}}|j|||d|d||jdS(u"Set pixel (x,y) to the given coloriufillN(u_GraphWin__checkOpenutoScreenu create_lineu_GraphWin__autoflush(uselfuxuyucoloruxsuys((u#/home/seanho/twu/python/graphics.pyuplots  cCs8|j|j|||d|d||jdS(uNSet pixel raw (independent of window coordinates) pixel (x,y) to coloriufillN(u_GraphWin__checkOpenu create_lineu_GraphWin__autoflush(uselfuxuyucolor((u#/home/seanho/twu/python/graphics.pyu plotPixels  cCs|j|jdS(uUpdate drawing to the windowN(u_GraphWin__checkOpenuupdate_idletasks(uself((u#/home/seanho/twu/python/graphics.pyuflushs cCs|jd|_d|_xT|jdks=|jdkrr|j|jrbtdntjdqW|j|j|j\}}d|_d|_t ||S(uKWait for mouse click and return Point object representing the clickugetMouse in closed windowg?N( uupdateuNoneumouseXumouseYuisClosedu GraphicsErrorutimeusleeputoWorlduPoint(uselfuxuy((u#/home/seanho/twu/python/graphics.pyugetMouses       cCs|jrtdn|j|jdkr|jdkr|j|j|j\}}d|_d|_t||SdSdS(uUReturn last mouse click or None if mouse has not been clicked since last callucheckMouse in closed windowN(uisClosedu GraphicsErroruupdateumouseXuNoneumouseYutoWorlduPoint(uselfuxuy((u#/home/seanho/twu/python/graphics.pyu checkMouses     cCs|jS(uReturn the height of the window(uheight(uself((u#/home/seanho/twu/python/graphics.pyu getHeightscCs|jS(uReturn the width of the window(uwidth(uself((u#/home/seanho/twu/python/graphics.pyugetWidth"scCs0|j}|r"|jj||S||fSdS(N(utransuscreen(uselfuxuyutrans((u#/home/seanho/twu/python/graphics.pyutoScreen&s cCs0|j}|r"|jj||S||fSdS(N(utransuworld(uselfuxuyutrans((u#/home/seanho/twu/python/graphics.pyutoWorld-s cCs ||_dS(N(u_mouseCallback(uselfufunc((u#/home/seanho/twu/python/graphics.pyusetMouseHandler4scCsD|j|_|j|_|jr@|jt|j|jndS(N(uxumouseXuyumouseYu_mouseCallbackuPoint(uselfue((u#/home/seanho/twu/python/graphics.pyu_onClick7s   NT(u__name__u __module__u__doc__uTrueu__init__u_GraphWin__checkOpenu setBackgroundu setCoordsucloseuisCloseduisOpenu_GraphWin__autoflushuplotu plotPixeluflushugetMouseu checkMouseu getHeightugetWidthutoScreenutoWorldusetMouseHandleru_onClick(u __locals__((u#/home/seanho/twu/python/graphics.pyuGraphWins*                 uGraphWincBs/|EeZdZdZdZdZdS(u1Internal class for 2-D coordinate transformationsc CsX||}||}||_||_|t|d|_|t|d|_dS(Ni(uxbaseuybaseufloatuxscaleuyscale( uselfuwuhuxlowuylowuxhighuyhighuxspanuyspan((u#/home/seanho/twu/python/graphics.pyu__init__As     cCsF||j|j}|j||j}t|dt|dfS(Ng?(uxbaseuxscaleuybaseuyscaleuint(uselfuxuyuxsuys((u#/home/seanho/twu/python/graphics.pyuscreenLscCs2||j|j}|j||j}||fS(N(uxscaleuxbaseuybaseuyscale(uselfuxsuysuxuy((u#/home/seanho/twu/python/graphics.pyuworldRsN(u__name__u __module__u__doc__u__init__uscreenuworld(u __locals__((u#/home/seanho/twu/python/graphics.pyu Transform=s  u Transformuufillublackuoutlineu1uwidthunoneuarrowutextucenterujustifyu helveticai unormalufontcBsn|EeZdZdZdZdZdZdZdZdZ dZ d Z d Z d S( u2Generic base class for all of the drawable objectscCsDd|_d|_i}x|D]}t|||}|j|j|j\}}|j||j|qW|j|tj|S(N(upointsutoScreenuxuyuappenduGraphWinucreate_polygon(uselfucanvasuoptionsuargsupuxuy((u#/home/seanho/twu/python/graphics.pyu_draw]s    N(u__name__u __module__u__init__ucloneu getPointsu_moveu_draw(u __locals__((u#/home/seanho/twu/python/graphics.pyuPolygonHs     uPolygoncBsq|EeZdZdZdZdZdZdZdZdZ dZ d Z d Z d S( cCsYtj|ddddg|j||j|_|jtd|j|_dS(Nujustifyufillutextufontuoutline(uGraphicsObjectu__init__usetTextucloneuanchorusetFilluDEFAULT_CONFIGu setOutline(uselfuputext((u#/home/seanho/twu/python/graphics.pyu__init__hs  cCs:|j}|j|j|j\}}|j|||S(N(uanchorutoScreenuxuyu create_text(uselfucanvasuoptionsupuxuy((u#/home/seanho/twu/python/graphics.pyu_drawos cCs|jj||dS(N(uanchorumove(uselfudxudy((u#/home/seanho/twu/python/graphics.pyu_movetscCs/t|j|jd}|jj|_|S(Nutext(uTextuanchoruconfigucopy(uselfuother((u#/home/seanho/twu/python/graphics.pyuclonewscCs|jd|dS(Nutext(u _reconfig(uselfutext((u#/home/seanho/twu/python/graphics.pyusetText|scCs |jdS(Nutext(uconfig(uself((u#/home/seanho/twu/python/graphics.pyugetTextscCs |jjS(N(uanchoruclone(uself((u#/home/seanho/twu/python/graphics.pyu getAnchorscCsN|dkr>|jd\}}}|jd|||fn ttdS(Nu helveticauarialucourieru times romanufont(u helveticauarialucourieru times roman(uconfigu _reconfigu GraphicsErroru BAD_OPTION(uselfufaceufusub((u#/home/seanho/twu/python/graphics.pyusetFaces cCs^d|kodknrN|jd\}}}|jd|||fn ttdS(Nii$ufont(uconfigu _reconfigu GraphicsErroru BAD_OPTION(uselfusizeufusub((u#/home/seanho/twu/python/graphics.pyusetSizescCsN|dkr>|jd\}}}|jd|||fn ttdS(Nuboldunormaluitalicu bold italicufont(uboldunormaluitalicu bold italic(uconfigu _reconfigu GraphicsErroru BAD_OPTION(uselfustyleufusub((u#/home/seanho/twu/python/graphics.pyusetStyles cCs|j|dS(N(usetFill(uselfucolor((u#/home/seanho/twu/python/graphics.pyu setTextColorsN( u__name__u __module__u__init__u_drawu_moveucloneusetTextugetTextu getAnchorusetFaceusetSizeusetStyleu setTextColor(u __locals__((u#/home/seanho/twu/python/graphics.pyuTextfs           uTextcBs|EeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd S(cCsvtj|g|j|_||_tjt|_|jj dd|_ d|_ t d|_ d|_dS(Nuugrayublackufont(uGraphicsObjectu__init__ucloneuanchoruwidthutku StringVaru_rootutextusetufillucoloruDEFAULT_CONFIGufontuNoneuentry(uselfupuwidth((u#/home/seanho/twu/python/graphics.pyu__init__s    c Cs|j}|j|j|j\}}tj|j}tj|d|jd|j d|j d|j d|j |_ |j j|j||d|S(Nuwidthu textvariableubgufgufontuwindow(uanchorutoScreenuxuyutkuFrameumasteruEntryuwidthutextufillucolorufontuentryupacku create_window(uselfucanvasuoptionsupuxuyufrm((u#/home/seanho/twu/python/graphics.pyu_draws       cCs |jjS(N(utextuget(uself((u#/home/seanho/twu/python/graphics.pyugetTextscCs|jj||dS(N(uanchorumove(uselfudxudy((u#/home/seanho/twu/python/graphics.pyu_movescCs |jjS(N(uanchoruclone(uself((u#/home/seanho/twu/python/graphics.pyu getAnchorscCs_t|j|j}|jj|_tj|_|jj|jj |j |_ |S(N( uEntryuanchoruwidthuconfigucopyutku StringVarutextusetugetufill(uselfuother((u#/home/seanho/twu/python/graphics.pyuclones  cCs|jj|dS(N(utextuset(uselfut((u#/home/seanho/twu/python/graphics.pyusetTextscCs,||_|jr(|jjd|ndS(Nubg(ufilluentryuconfig(uselfucolor((u#/home/seanho/twu/python/graphics.pyusetFills  cCsNt|j}|||=sJU$     k8WM  ,