B
    š¹èTër  ã               @   s€  d Z dZddlZddlZddlZyddlZW n   ddlZY nX G dd„ deƒZ	dZ
dZdZd	Ze ¡ Ze ¡  d
d„ ZG dd„ dejƒZG dd„ dƒZddddddddœZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd „ d eƒZG d!d"„ d"eƒZG d#d$„ d$eƒZG d%d&„ d&eƒZG d'd(„ d(eƒZG d)d*„ d*eƒZG d+d,„ d,eƒZ d-d.„ Z!d/d0„ Z"e#d1kr|e"ƒ  dS )2a·  Simple 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 referencez4.3é    Nc               @   s   e Zd ZdZdS )ÚGraphicsErrorz3Generic error class for graphics module exceptions.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú8/Users/rego/Desktop/Mouse Example - Graphics/graphics.pyr   §   s   r   zObject currently drawnz Object doesn't support operationzIllegal option valuez!Graphics thread quit unexpectedlyc               C   s   t  ¡  d S )N)Ú_rootÚupdater   r   r   r   r
   ³   s    r
   c               @   sÞ   e Zd ZdZd9dd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zd:dd„Zd;dd„Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8S )<ÚGraphWinz8A GraphWin is a toplevel window for displaying graphics.úGraphics WindowéÈ   Tc             C   sÂ   t  t¡}| d| j¡ t jj| |||d | j |¡ |  	¡  | 
dd¡ d| _g | _d | _d | _|  d| j¡ |  d| j¡ || _|| _|| _d | _d | _d| _| ¡  d| _|r¾t ¡  d S )	NÚWM_DELETE_WINDOW)ÚwidthÚheightr   Úblackz
<Button-1>z<Key>FÚ )ÚtkÚToplevelr	   ÚprotocolÚcloseÚCanvasÚ__init__ÚmasterÚtitleÚpackÚ	resizableÚ
foregroundÚitemsÚmouseXÚmouseYÚbindÚ_onClickÚbind_allÚ_onKeyr   r   Ú	autoflushÚ_mouseCallbackÚtransÚclosedÚliftÚlastKeyr
   )Úselfr   r   r   r%   r   r   r   r   r   ½   s,    
 zGraphWin.__init__c             C   s   | j rtdƒ‚d S )Nzwindow is closed)r(   r   )r+   r   r   r   Z__checkOpenÕ   s    zGraphWin.__checkOpenc             C   s   |j | _d S )N)Úkeysymr*   )r+   Zevntr   r   r   r$   Ù   s    zGraphWin._onKeyc             C   s    |   ¡  | j|d |  ¡  dS )z"Set background color of the window)ÚbgN)Ú_GraphWin__checkOpenÚconfigÚ_GraphWin__autoflush)r+   Úcolorr   r   r   ÚsetBackgroundÝ   s    zGraphWin.setBackgroundc             C   s$   t | j| j||||ƒ| _|  ¡  dS )ztSet coordinates of window to run from (x1,y1) in the
        lower-left corner to (x2,y2) in the upper-right corner.N)Ú	Transformr   r   r'   Úredraw)r+   Úx1Úy1Úx2Úy2r   r   r   Ú	setCoordsã   s    zGraphWin.setCoordsc             C   s&   | j r
dS d| _ | j ¡  |  ¡  dS )zClose the windowNT)r(   r   Údestroyr0   )r+   r   r   r   r   é   s
     
zGraphWin.closec             C   s   | j S )N)r(   )r+   r   r   r   ÚisClosedò   s    zGraphWin.isClosedc             C   s   | j  S )N)r(   )r+   r   r   r   ÚisOpenö   s    zGraphWin.isOpenc             C   s   | j rt ¡  d S )N)r%   r	   r
   )r+   r   r   r   Z__autoflushú   s    zGraphWin.__autoflushr   c             C   s<   |   ¡  |  ||¡\}}| j|||d ||d |  ¡  dS )z"Set pixel (x,y) to the given coloré   )ÚfillN)r.   ÚtoScreenÚcreate_liner0   )r+   ÚxÚyr1   ÚxsÚysr   r   r   Úplotÿ   s    zGraphWin.plotc             C   s,   |   ¡  | j|||d ||d |  ¡  dS )zNSet pixel raw (independent of window coordinates) pixel
        (x,y) to colorr=   )r>   N)r.   r@   r0   )r+   rA   rB   r1   r   r   r   Ú	plotPixel  s    zGraphWin.plotPixelc             C   s   |   ¡  |  ¡  dS )zUpdate drawing to the windowN)r.   Úupdate_idletasks)r+   r   r   r   Úflush  s    zGraphWin.flushc             C   sz   |   ¡  d| _d| _x:| jdks*| jdkrN|   ¡  |  ¡ rBtdƒ‚t d¡ qW |  | j| j¡\}}d| _d| _t||ƒS )zKWait for mouse click and return Point object representing
        the clickNzgetMouse in closed windowgš™™™™™¹?)	r
   r   r    r;   r   ÚtimeÚsleepÚtoWorldÚPoint)r+   rA   rB   r   r   r   ÚgetMouse  s     zGraphWin.getMousec             C   s^   |   ¡ rtdƒ‚|  ¡  | jdkrV| jdkrV|  | j| j¡\}}d| _d| _t||ƒS dS dS )zUReturn last mouse click or None if mouse has
        not been clicked since last callzcheckMouse in closed windowN)r;   r   r
   r   r    rK   rL   )r+   rA   rB   r   r   r   Ú
checkMouse!  s    
zGraphWin.checkMousec             C   sH   d| _ x0| j dkr6|  ¡  |  ¡ r*tdƒ‚t d¡ qW | j }d| _ |S )z7Wait for user to press a key and return it as a string.r   zgetKey in closed windowgš™™™™™¹?)r*   r
   r;   r   rI   rJ   )r+   Úkeyr   r   r   ÚgetKey/  s     zGraphWin.getKeyc             C   s(   |   ¡ rtdƒ‚|  ¡  | j}d| _|S )zAReturn last key pressed or None if no key pressed since last callzcheckKey in closed windowr   )r;   r   r
   r*   )r+   rO   r   r   r   ÚcheckKey;  s    zGraphWin.checkKeyc             C   s   | j S )zReturn the height of the window)r   )r+   r   r   r   Ú	getHeightD  s    zGraphWin.getHeightc             C   s   | j S )zReturn the width of the window)r   )r+   r   r   r   ÚgetWidthH  s    zGraphWin.getWidthc             C   s$   | j }|r| j  ||¡S ||fS d S )N)r'   Úscreen)r+   rA   rB   r'   r   r   r   r?   L  s    zGraphWin.toScreenc             C   s$   | j }|r| j  ||¡S ||fS d S )N)r'   Úworld)r+   rA   rB   r'   r   r   r   rK   S  s    zGraphWin.toWorldc             C   s
   || _ d S )N)r&   )r+   Úfuncr   r   r   ÚsetMouseHandlerZ  s    zGraphWin.setMouseHandlerc             C   s.   |j | _|j| _| jr*|  t|j |jƒ¡ d S )N)rA   r   rB   r    r&   rL   )r+   Úer   r   r   r"   ]  s    zGraphWin._onClickc             C   s   | j  |¡ d S )N)r   Úappend)r+   Úitemr   r   r   ÚaddItemc  s    zGraphWin.addItemc             C   s   | j  |¡ d S )N)r   Úremove)r+   rZ   r   r   r   ÚdelItemf  s    zGraphWin.delItemc             C   s6   x(| j d d … D ]}| ¡  | | ¡ qW |  ¡  d S )N)r   ÚundrawÚdrawr
   )r+   rZ   r   r   r   r4   i  s    zGraphWin.redrawN)r   r   r   T)r   )r   )r   r   r   r   r   r.   r$   r2   r9   r   r;   r<   r0   rE   rF   rH   rM   rN   rP   rQ   rR   rS   r?   rK   rW   r"   r[   r]   r4   r   r   r   r   r   ¹   s6    
	

	r   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r3   z1Internal class for 2-D coordinate transformationsc       	      C   sD   || }|| }|| _ || _|t|d ƒ | _|t|d ƒ | _d S )Nr=   )ÚxbaseÚybaseÚfloatÚxscaleÚyscale)	r+   ÚwÚhZxlowZylowZxhighZyhighZxspanZyspanr   r   r   r   t  s    zTransform.__init__c             C   s8   || j  | j }| j| | j }t|d ƒt|d ƒfS )Ng      à?)r`   rc   ra   rd   Úint)r+   rA   rB   rC   rD   r   r   r   rT     s    zTransform.screenc             C   s(   || j  | j }| j|| j  }||fS )N)rc   r`   ra   rd   )r+   rC   rD   rA   rB   r   r   r   rU   …  s    zTransform.worldN)r   r   r   r   r   rT   rU   r   r   r   r   r3   p  s   r3   r   r   Ú1ÚnoneÚcenter)Ú	helveticaé   Únormal)r>   Úoutliner   ÚarrowÚtextÚjustifyÚfontc               @   s`   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚGraphicsObjectz2Generic base class for all of the drawable objectsc             C   s4   d | _ d | _i }x|D ]}t| ||< qW || _d S )N)ÚcanvasÚidÚDEFAULT_CONFIGr/   )r+   Úoptionsr/   Úoptionr   r   r   r   œ  s    
zGraphicsObject.__init__c             C   s   |   d|¡ dS )zSet interior color to colorr>   N)Ú	_reconfig)r+   r1   r   r   r   ÚsetFill¬  s    zGraphicsObject.setFillc             C   s   |   d|¡ dS )zSet outline color to colorrn   N)ry   )r+   r1   r   r   r   Ú
setOutline°  s    zGraphicsObject.setOutlinec             C   s   |   d|¡ dS )zSet line weight to widthr   N)ry   )r+   r   r   r   r   ÚsetWidth´  s    zGraphicsObject.setWidthc             C   sZ   | j r| j  ¡ sttƒ‚| ¡ r(tdƒ‚|| _ |  || j¡| _| | ¡ |jrVt	 
¡  dS )z×Draw the object in graphwin, which should be a GraphWin
        object.  A GraphicsObject may only be drawn into one
        window. Raises an error if attempt made to draw an object that
        is already visible.zCan't draw to closed windowN)rt   r;   r   ÚOBJ_ALREADY_DRAWNÚ_drawr/   ru   r[   r%   r	   r
   )r+   Zgraphwinr   r   r   r_   ¸  s      
zGraphicsObject.drawc             C   sN   | j s
dS | j  ¡ s>| j  | j¡ | j  | ¡ | j jr>t ¡  d| _ d| _dS )z`Undraw the object (i.e. hide it). Returns silently if the
        object is not currently drawn.N)rt   r;   Údeleteru   r]   r%   r	   r
   )r+   r   r   r   r^   È  s     
zGraphicsObject.undrawc             C   sl   |   ||¡ | j}|rh| ¡ sh|j}|r@||j }| |j }n|}|}| j | j||¡ |jrht	 
¡  dS )zGmove object dx units in x direction and dy units in y
        directionN)Ú_movert   r;   r'   rc   rd   Úmoveru   r%   r	   r
   )r+   ÚdxÚdyrt   r'   rA   rB   r   r   r   r   ×  s    
zGraphicsObject.movec             C   sT   || j krttƒ‚| j }|||< | jrP| j ¡ sP| j | j|¡ | jjrPt 	¡  d S )N)
r/   r   ÚUNSUPPORTED_METHODrt   r;   Ú
itemconfigru   r%   r	   r
   )r+   rx   Zsettingrw   r   r   r   ry   ê  s    
zGraphicsObject._reconfigc             C   s   dS )z\draws appropriate figure on canvas with options provided
        Returns Tk id of item drawnNr   )r+   rt   rw   r   r   r   r~   ø  s    zGraphicsObject._drawc             C   s   dS )z7updates internal state of object to move it dx,dy unitsNr   )r+   r‚   rƒ   r   r   r   r€   þ  s    zGraphicsObject._moveN)r   r   r   r   r   rz   r{   r|   r_   r^   r   ry   r~   r€   r   r   r   r   rs   –  s   rs   c               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )rL   c             C   s(   t  | ddg¡ | j| _|| _|| _d S )Nrn   r>   )rs   r   r{   rz   rA   rB   )r+   rA   rB   r   r   r   r     s    zPoint.__init__c             C   s.   |  | j| j¡\}}| |||d |d |¡S )Nr=   )r?   rA   rB   Úcreate_rectangle)r+   rt   rw   rA   rB   r   r   r   r~   
  s    zPoint._drawc             C   s   | j | | _ | j| | _d S )N)rA   rB   )r+   r‚   rƒ   r   r   r   r€     s    zPoint._movec             C   s   t | j| jƒ}| j ¡ |_|S )N)rL   rA   rB   r/   Úcopy)r+   Úotherr   r   r   Úclone  s    zPoint.clonec             C   s   | j S )N)rA   )r+   r   r   r   ÚgetX  ó    z
Point.getXc             C   s   | j S )N)rB   )r+   r   r   r   ÚgetY  r‹   z
Point.getYN)	r   r   r   r   r~   r€   r‰   rŠ   rŒ   r   r   r   r   rL     s   rL   c               @   s>   e Zd Zdddgfdd„Zdd„ Zdd	„ Zd
d„ Zdd„ ZdS )Ú_BBoxrn   r   r>   c             C   s$   t  | |¡ | ¡ | _| ¡ | _d S )N)rs   r   r‰   Úp1Úp2)r+   rŽ   r   rw   r   r   r   r     s    
z_BBox.__init__c             C   sD   | j j| | j _| j j| | j _| jj| | j_| jj| | j_d S )N)rŽ   rA   rB   r   )r+   r‚   rƒ   r   r   r   r€   #  s    z_BBox._movec             C   s
   | j  ¡ S )N)rŽ   r‰   )r+   r   r   r   ÚgetP1)  r‹   z_BBox.getP1c             C   s
   | j  ¡ S )N)r   r‰   )r+   r   r   r   ÚgetP2+  r‹   z_BBox.getP2c             C   s.   | j }| j}t|j|j d |j|j d ƒS )Ng       @)rŽ   r   rL   rA   rB   )r+   rŽ   r   r   r   r   Ú	getCenter-  s    z_BBox.getCenterN)r   r   r   r   r€   r   r‘   r’   r   r   r   r   r     s
   r   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú	Rectanglec             C   s   t  | ||¡ d S )N)r   r   )r+   rŽ   r   r   r   r   r   4  s    zRectangle.__init__c       	      C   sF   | j }| j}| |j|j¡\}}| |j|j¡\}}| |||||¡S )N)rŽ   r   r?   rA   rB   r†   )	r+   rt   rw   rŽ   r   r5   r6   r7   r8   r   r   r   r~   7  s
    zRectangle._drawc             C   s   t | j| jƒ}| j ¡ |_|S )N)r“   rŽ   r   r/   r‡   )r+   rˆ   r   r   r   r‰   >  s    zRectangle.cloneN)r   r   r   r   r~   r‰   r   r   r   r   r“   2  s   r“   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚOvalc             C   s   t  | ||¡ d S )N)r   r   )r+   rŽ   r   r   r   r   r   E  s    zOval.__init__c             C   s   t | j| jƒ}| j ¡ |_|S )N)r”   rŽ   r   r/   r‡   )r+   rˆ   r   r   r   r‰   H  s    z
Oval.clonec       	      C   sF   | j }| j}| |j|j¡\}}| |j|j¡\}}| |||||¡S )N)rŽ   r   r?   rA   rB   Úcreate_oval)	r+   rt   rw   rŽ   r   r5   r6   r7   r8   r   r   r   r~   M  s
    z
Oval._drawN)r   r   r   r   r‰   r~   r   r   r   r   r”   C  s   r”   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚCirclec             C   sD   t |j| |j| ƒ}t |j| |j| ƒ}t | ||¡ || _d S )N)rL   rA   rB   r”   r   Úradius)r+   rj   r—   rŽ   r   r   r   r   r   V  s    zCircle.__init__c             C   s    t |  ¡ | jƒ}| j ¡ |_|S )N)r–   r’   r—   r/   r‡   )r+   rˆ   r   r   r   r‰   \  s    zCircle.clonec             C   s   | j S )N)r—   )r+   r   r   r   Ú	getRadiusa  s    zCircle.getRadiusN)r   r   r   r   r‰   r˜   r   r   r   r   r–   T  s   r–   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚLinec             C   s0   t  | ||dddg¡ |  td ¡ | j| _d S )Nro   r>   r   rn   )r   r   rz   rv   r{   )r+   rŽ   r   r   r   r   r   f  s    zLine.__init__c             C   s   t | j| jƒ}| j ¡ |_|S )N)r™   rŽ   r   r/   r‡   )r+   rˆ   r   r   r   r‰   k  s    z
Line.clonec       	      C   sF   | j }| j}| |j|j¡\}}| |j|j¡\}}| |||||¡S )N)rŽ   r   r?   rA   rB   r@   )	r+   rt   rw   rŽ   r   r5   r6   r7   r8   r   r   r   r~   p  s
    z
Line._drawc             C   s    |dkrt tƒ‚|  d|¡ d S )N)ÚfirstÚlastÚbothri   ro   )r   Ú
BAD_OPTIONry   )r+   rx   r   r   r   ÚsetArroww  s    zLine.setArrowN)r   r   r   r   r‰   r~   rž   r   r   r   r   r™   d  s   r™   c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚPolygonc             G   sP   t |ƒdkr(t|d ƒtg ƒkr(|d }tttj|ƒƒ| _t | dddg¡ d S )Nr=   r   rn   r   r>   )	ÚlenÚtypeÚlistÚmaprL   r‰   Úpointsrs   r   )r+   r¤   r   r   r   r     s     zPolygon.__init__c             C   s   t | jŽ }| j ¡ |_|S )N)rŸ   r¤   r/   r‡   )r+   rˆ   r   r   r   r‰   †  s    
zPolygon.clonec             C   s   t ttj| jƒƒS )N)r¢   r£   rL   r‰   r¤   )r+   r   r   r   Ú	getPoints‹  s    zPolygon.getPointsc             C   s    x| j D ]}| ||¡ qW d S )N)r¤   r   )r+   r‚   rƒ   Úpr   r   r   r€   Ž  s    zPolygon._movec             C   sR   |g}x6| j D ],}| |j|j¡\}}| |¡ | |¡ qW | |¡ tj|Ž S )N)r¤   r?   rA   rB   rY   r   Úcreate_polygon)r+   rt   rw   Úargsr¦   rA   rB   r   r   r   r~   ’  s    

zPolygon._drawN)r   r   r   r   r‰   r¥   r€   r~   r   r   r   r   rŸ   }  s
   rŸ   c               @   sd   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚTextc             C   sB   t  | ddddg¡ |  |¡ | ¡ | _|  td ¡ | j| _d S )Nrq   r>   rp   rr   rn   )rs   r   ÚsetTextr‰   Úanchorrz   rv   r{   )r+   r¦   rp   r   r   r   r     s
    

zText.__init__c             C   s(   | j }| |j|j¡\}}| |||¡S )N)r«   r?   rA   rB   Úcreate_text)r+   rt   rw   r¦   rA   rB   r   r   r   r~   ¤  s    z
Text._drawc             C   s   | j  ||¡ d S )N)r«   r   )r+   r‚   rƒ   r   r   r   r€   ©  s    z
Text._movec             C   s"   t | j| jd ƒ}| j ¡ |_|S )Nrp   )r©   r«   r/   r‡   )r+   rˆ   r   r   r   r‰   ¬  s    z
Text.clonec             C   s   |   d|¡ d S )Nrp   )ry   )r+   rp   r   r   r   rª   ±  s    zText.setTextc             C   s
   | j d S )Nrp   )r/   )r+   r   r   r   ÚgetText´  s    zText.getTextc             C   s
   | j  ¡ S )N)r«   r‰   )r+   r   r   r   Ú	getAnchor·  s    zText.getAnchorc             C   s8   |dkr,| j d \}}}|  d|||f¡ nttƒ‚d S )N)rk   ÚarialÚcourierztimes romanrr   )r/   ry   r   r   )r+   ÚfaceÚfÚsÚbr   r   r   ÚsetFaceº  s    zText.setFacec             C   sH   d|  krdkr<n n$| j d \}}}|  d|||f¡ nttƒ‚d S )Né   é$   rr   )r/   ry   r   r   )r+   Úsizer²   r³   r´   r   r   r   ÚsetSizeÁ  s    zText.setSizec             C   s8   |dkr,| j d \}}}|  d|||f¡ nttƒ‚d S )N)Úboldrm   Úitaliczbold italicrr   )r/   ry   r   r   )r+   Ústyler²   r³   r´   r   r   r   ÚsetStyleÈ  s    zText.setStylec             C   s   |   |¡ d S )N)rz   )r+   r1   r   r   r   ÚsetTextColorÏ  s    zText.setTextColorN)r   r   r   r   r~   r€   r‰   rª   r­   r®   rµ   r¹   r½   r¾   r   r   r   r   r©   ›  s   r©   c               @   st   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚEntryc             C   sT   t  | g ¡ | ¡ | _|| _t t¡| _| j 	d¡ d| _
d| _td | _d | _d S )Nr   Úgrayr   rr   )rs   r   r‰   r«   r   r   Ú	StringVarr	   rp   Úsetr>   r1   rv   rr   Úentry)r+   r¦   r   r   r   r   r   Õ  s    

zEntry.__init__c             C   sb   | j }| |j|j¡\}}t |j¡}tj|| j| j	| j
| j| jd| _| j ¡  |j|||dS )N)r   Útextvariabler-   Úfgrr   )Úwindow)r«   r?   rA   rB   r   ÚFramer   r¿   r   rp   r>   r1   rr   rÃ   r   Úcreate_window)r+   rt   rw   r¦   rA   rB   Zfrmr   r   r   r~   á  s    
zEntry._drawc             C   s
   | j  ¡ S )N)rp   Úget)r+   r   r   r   r­   ï  s    zEntry.getTextc             C   s   | j  ||¡ d S )N)r«   r   )r+   r‚   rƒ   r   r   r   r€   ò  s    zEntry._movec             C   s
   | j  ¡ S )N)r«   r‰   )r+   r   r   r   r®   õ  s    zEntry.getAnchorc             C   sB   t | j| jƒ}| j ¡ |_t ¡ |_|j | j 	¡ ¡ | j
|_
|S )N)r¿   r«   r   r/   r‡   r   rÁ   rp   rÂ   rÉ   r>   )r+   rˆ   r   r   r   r‰   ø  s    
zEntry.clonec             C   s   | j  |¡ d S )N)rp   rÂ   )r+   Útr   r   r   rª      s    zEntry.setTextc             C   s   || _ | jr| jj|d d S )N)r-   )r>   rÃ   r/   )r+   r1   r   r   r   rz     s    zEntry.setFillc             C   s6   t | jƒ}|||< t|ƒ| _| jr2| jj| jd d S )N)rr   )r¢   rr   ÚtuplerÃ   r/   )r+   ÚwhichÚvaluerr   r   r   r   Ú_setFontComponent
  s
    

zEntry._setFontComponentc             C   s"   |dkr|   d|¡ nttƒ‚d S )N)rk   r¯   r°   ztimes romanr   )rÎ   r   r   )r+   r±   r   r   r   rµ     s    zEntry.setFacec             C   s2   d|  krdkr&n n|   d|¡ nttƒ‚d S )Nr¶   r·   r=   )rÎ   r   r   )r+   r¸   r   r   r   r¹     s    zEntry.setSizec             C   s"   |dkr|   d|¡ nttƒ‚d S )N)rº   rm   r»   zbold italicé   )rÎ   r   r   )r+   r¼   r   r   r   r½     s    zEntry.setStylec             C   s   || _ | jr| jj|d d S )N)rÅ   )r1   rÃ   r/   )r+   r1   r   r   r   r¾   $  s    zEntry.setTextColorN)r   r   r   r   r~   r­   r€   r®   r‰   rª   rz   rÎ   rµ   r¹   r½   r¾   r   r   r   r   r¿   Ó  s   r¿   c               @   sl   e Zd ZdZi Zdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚImager   c             G   sj   t  | g ¡ | ¡ | _tj| _tjd t_t|ƒdkrLtj	|d t
d| _n|\}}tj	t
||d| _d S )Nr=   r   )Úfiler   )r   r   r   )rs   r   r‰   r«   rÐ   ÚidCountÚimageIdr    r   Ú
PhotoImager	   Úimg)r+   r¦   Zpixmapr   r   r   r   r   r   /  s    
zImage.__init__c             C   s:   | j }| |j|j¡\}}| j| j| j< |j||| jdS )N)Úimage)r«   r?   rA   rB   rÕ   Ú
imageCacherÓ   Úcreate_image)r+   rt   rw   r¦   rA   rB   r   r   r   r~   :  s    zImage._drawc             C   s   | j  ||¡ d S )N)r«   r   )r+   r‚   rƒ   r   r   r   r€   @  s    zImage._movec             C   s2   y| j | j= W n tk
r"   Y nX t | ¡ d S )N)r×   rÓ   ÚKeyErrorrs   r^   )r+   r   r   r   r^   C  s
    zImage.undrawc             C   s
   | j  ¡ S )N)r«   r‰   )r+   r   r   r   r®   J  s    zImage.getAnchorc             C   s:   t tddƒddƒ}| j ¡ |_| j ¡ |_| j ¡ |_|S )Nr   )rÐ   rL   rÕ   r‡   r«   r‰   r/   )r+   rˆ   r   r   r   r‰   M  s
    zImage.clonec             C   s
   | j  ¡ S )z(Returns the width of the image in pixels)rÕ   r   )r+   r   r   r   rS   T  s    zImage.getWidthc             C   s
   | j  ¡ S )z)Returns the height of the image in pixels)rÕ   r   )r+   r   r   r   rR   X  s    zImage.getHeightc             C   sV   | j  ||¡}t|ƒtdƒkr(|||gS t|ƒtdƒkr@t|ƒS ttt| ¡ ƒƒS dS )zjReturns a list [r,g,b] with the RGB color values for pixel (x,y)
        r,g,b are in range(256)

        r   )r   r   r   N)rÕ   rÉ   r¡   r¢   r£   rg   Úsplit)r+   rA   rB   rÍ   r   r   r   ÚgetPixel\  s    
zImage.getPixelc             C   s   | j  d| d ||f¡ dS )z5Sets pixel (x,y) to the given color
        
        Ú{Ú}N)rÕ   Úput)r+   rA   rB   r1   r   r   r   ÚsetPixelj  s    zImage.setPixelc             C   s2   t j |¡\}}| d¡d }| jj||d dS )z}Saves the pixmap image to filename.
        The format for the save image is determined from the filname extension.

        Ú.éÿÿÿÿ)ÚformatN)ÚosÚpathrÚ   rÕ   Úwrite)r+   Úfilenamerä   ÚnameÚextr   r   r   Úsaveq  s    z
Image.saveN)r   r   r   rÒ   r×   r   r~   r€   r^   r®   r‰   rS   rR   rÛ   rß   ré   r   r   r   r   rÐ   *  s   rÐ   c             C   s   d| ||f S )zvr,g,b are intensities of red, green, and blue in range(256)
    Returns color specifier string for the resulting colorz#%02x%02x%02xr   )ÚrÚgr´   r   r   r   Ú	color_rgb|  s    rì   c              C   sÜ  t ƒ } |  dddd¡ ttddƒdƒ}| | ¡ ttddƒtddƒtddƒƒ}| | ¡ ttdd	ƒdƒ}| | ¡ |  ¡  | d
¡ | 	d¡ | 
d¡ d}x(| ¡ D ]}|d| ¡ | ¡ f  }q¦W | | ¡ ¡ | d¡ | d¡ | dd¡ |  ¡  | dd¡ d}x*| ¡ D ]}|d| ¡ | ¡ f  }qW | |¡ |  ¡  | ¡  | ¡  | d¡ |  ¡  | d¡ |  ¡  | d¡ |  ¡  | d¡ |  ¡  | d¡ |  ¡  | d¡ | d¡ |  ¡  |  ¡  d S )Nr   é
   r¶   zCentered Textr=   é   rÏ   é   é   ZredZbluer   z(%0.1f,%0.1f) ZgreenzSpam!rº   rm   r»   zbold italicé   r¯   é   )r   r9   r©   rL   r_   rŸ   r¿   rM   rz   r{   r|   r¥   rŠ   rŒ   rª   r­   r   r^   r½   r¹   rµ   r   )ÚwinrÊ   r¦   rX   r³   Úptr   r   r   Útest  sT    















rõ   Ú__main__)$r   Ú__version__rI   rã   ÚsysÚtkinterr   ÚTkinterÚ	Exceptionr   r}   r„   r   ZDEAD_THREADÚTkr	   Úwithdrawr
   r   r   r3   rv   rs   rL   r   r“   r”   r–   r™   rŸ   r©   r¿   rÐ   rì   rõ   r   r   r   r   r   Ú<module>=   sN   ] 8m8WR,
