B
    È S]·7  ã               @   s  d Z ddlZddlZddlT ddlmZmZ ddlmZ ddl	m
Z
 ddlm Z ddlZej ej e¡¡ZejdkZd	Zd
ZdZdZdZddefZdZdddgZdZdZdddddddddddgZ dd „ Z!d!e fd"efd#ej ffZ"G d$d%„ d%e#ƒZ$d&d'„ Z%e&d(kre%ƒ  dS ))a˜  
  ----------------------------------------------
      turtleDemo - Help
  ----------------------------------------------

  This document has two sections:

  (1) How to use the demo viewer
  (2) How to add your own demos to the demo repository


  (1) How to use the demo viewer.

  Select a demoscript from the example menu.
  The (syntax colored) source code appears in the left
  source code window. IT CANNOT BE EDITED, but ONLY VIEWED!

  The demo viewer windows can be resized. The divider between text
  and canvas can be moved by grabbing it with the mouse. The text font
  size can be changed from the menu and with Control/Command '-'/'+'.
  It can also be changed on most systems with Control-mousewheel
  when the mouse is over the text.

  Press START button to start the demo.
  Stop execution by pressing the STOP button.
  Clear screen by pressing the CLEAR button.
  Restart by pressing the START button again.

  SPECIAL demos, such as clock.py are those which run EVENTDRIVEN.

      Press START button to start the demo.

      - Until the EVENTLOOP is entered everything works
      as in an ordinary demo script.

      - When the EVENTLOOP is entered, you control the
      application by using the mouse and/or keys (or it's
      controlled by some timer events)
      To stop it you can and must press the STOP button.

      While the EVENTLOOP is running, the examples menu is disabled.

      - Only after having pressed the STOP button, you may
      restart it or choose another example script.

   * * * * * * * *
   In some rare situations there may occur interferences/conflicts
   between events concerning the demo script and those concerning the
   demo-viewer. (They run in the same process.) Strange behaviour may be
   the consequence and in the worst case you must close and restart the
   viewer.
   * * * * * * * *


   (2) How to add your own demos to the demo repository

   - Place the file in the same directory as turtledemo/__main__.py
     IMPORTANT! When imported, the demo should not modify the system
     by calling functions in other modules, such as sys, tkinter, or
     turtle. Global variables should be initialized in main().

   - The code must contain a main() function which will
     be executed by the viewer (see provided example scripts).
     It may return a string which will be displayed in the Label below
     the source code window (when execution has finished.)

   - In order to run mydemo.py by itself, such as during development,
     add the following at the end of the file:

    if __name__ == '__main__':
        main()
        mainloop()  # keep window open

    python -m turtledemo.mydemo  # will then run it

   - If the demo is EVENT DRIVEN, main must return the string
     "EVENTLOOP". This informs the demo viewer that the script is
     still running and must be stopped by the user!

     If an "EVENTLOOP" demo runs by itself, as with clock, which uses
     ontimer, or minimal_hanoi, which loops by recursion, then the
     code should catch the turtle.Terminator exception that will be
     raised when the user presses the STOP button.  (Paint is not such
     a demo; it only acts in response to mouse clicks and movements.)
é    N)Ú*)ÚColorDelegatorÚcolor_config)Ú
Percolator)Ú	view_text)Ú__doc__Údarwiné   é   é   é   é   ÚArialé   )r   r   ZboldzLucida Consoleé
   Únormalé   éd   é   é	   é   é   é   é   é   é   é   c               C   s   dd„ t  t¡D ƒS )Nc             S   s.   g | ]&}|  d ¡r|d dkr|dd… ‘qS )z.pyr   Ú_Néýÿÿÿ)Úendswith)Ú.0Úentry© r"   ú$lib/python3.7/turtledemo/__main__.pyú
<listcomp>u   s    z%getExampleEntries.<locals>.<listcomp>)ÚosÚlistdirÚdemo_dirr"   r"   r"   r#   ÚgetExampleEntriest   s    r(   zTurtledemo helpzAbout turtledemozAbout turtle modulec               @   s¤   e Zd Zd(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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 ),Ú
DemoWindowNc          	   C   sJ  t ƒ  | _ }t_| d¡ | d| j¡ trbdd l}|j	ddddd 
t ¡ ¡ddg|j|jd	 |jdd
d |jdd
d |jd
dd
d |jddd
d |jddd
d t|tdd| _| jj|  | j¡ddd | jj|  | j¡ddd | jj|  | j¡ddd | j|d< ttdtdd}| |  |¡¡ | |  |¡¡ |jdddd t|d
ddddtd| _ t!|d t"d!d"| j#d#| _$t!|d$t"d!d"| j%d#| _&t!|d%t"d!d"| j'd#| _(| j jd
ddd&d' | j$jd
d
d(d) | j&jd
dd(d) | j(jd
dd(d) t)| j*ƒ +t,ƒ ¡ d*| _-d*| _.|r.|  /|¡ |  0t1t1t1d+d,¡ t2| _3d S )-NzPython turtle-graphics examplesZWM_DELETE_WINDOWr   Z	osascriptz-ez tell application "System Events"z>set frontmost of the first process whose unix id is {} to truezend tell)ÚstderrÚstdoutr	   )ÚweightéZ   )Zminsizer,   r
   r   )ÚreliefÚborderwidthZExamples)ÚmenuÚlabelÚ	underlineZFontsizeZHelpr0   r   z#ddd)ÚorientZ	sashwidthZ
sashreliefÚbgr   Znews)ÚrowZ
columnspanÚstickyz --- z#ddf)r   é   r   )ZheightÚtextr4   Úfontr/   r.   z START Zwhitez#fed)r8   r9   ÚfgZdisabledforegroundÚcommandz STOP z CLEAR )r   r   )r5   Úcolumnr6   ÚpadxZew)r5   r<   r6   FzChoose example from menuÚblack)4ZTkÚrootÚturtleÚ_rootÚtitleZwm_protocolÚ_destroyr   Ú
subprocessÚrunÚformatr%   ÚgetpidZDEVNULLZgrid_rowconfigureZgrid_columnconfigureÚMenuZRAISEDZmBarZadd_cascadeÚmakeLoadDemoMenuÚmakeFontMenuÚmakeHelpMenuZPanedWindowÚ
HORIZONTALZSOLIDÚaddÚmakeTextFrameÚmakeGraphFrameZgridZLabelZRIDGEÚ
output_lblZButtonÚbtnfontÚ	startDemoÚ	start_btnÚstopItÚstop_btnÚclearCanvasÚ	clear_btnr   r8   Zinsertfilterr   ÚdirtyÚexitflagÚloadfileÚ	configGUIÚDISABLEDÚSTARTUPÚstate)ÚselfÚfilenamer?   rD   Zpaner"   r"   r#   Ú__init__‚   sn    










zDemoWindow.__init__c             C   sP   | j  ¡ }| j  ¡ }| j  d| j|  | j ¡ | j  d| j|  | j ¡ d S )Ng      à?)Ú_canvasZwinfo_widthZwinfo_heightZxview_movetoÚ	canvwidthZyview_movetoÚ
canvheight)r_   ÚeventZcwidthZcheightr"   r"   r#   ÚonResizeÆ   s    

zDemoWindow.onResizec             C   s6  t |ƒ | _}t|ddddd | _}t|ƒ t|dd | _}|j|d< |jt	t
d	 t|d
td | _}|j|d< |jttd	 |j|d< |j|d< ttƒ|d< tr¬dnd}| d| | j¡ | d| | j¡ | d| | j¡ | d| | j¡ | d| j¡ | d| j¡ | d| j¡ |jt	tdd |S )Nr8   r   Znoneé-   )Únamer=   ZwrapÚwidthÚvbar)rh   r;   )ÚsideÚfillÚhbar)rh   r3   ZyscrollcommandZxscrollcommandr9   ZCommandZControlz
<%s-minus>z<%s-underscore>z
<%s-equal>z	<%s-plus>z<Control-MouseWheel>z<Control-Button-4>z<Control-Button-5>r	   )rk   rl   Úexpand)ZFrameÚ
text_frameZTextr8   r   Z	Scrollbarrj   ZyviewZpackZLEFTÚYrL   rm   ZxviewZBOTTOMÚXÚsetÚtupleÚtxtfontr   Zbind_allÚdecrease_sizeÚincrease_sizeÚbindÚupdate_mousewheelZBOTH)r_   r?   ro   r8   rj   rm   Zshortcutr"   r"   r#   rN   Ì   s.    



zDemoWindow.makeTextFramec             C   sŽ   |t j_d| _d| _t  |dd| j| j¡ t j_ | _}| ¡  |j 	d| j
¡ d|jd< t  ¡  | _}t j ||j¡ |j| _|gt j_|S )Niè  i   iX  z<Configure>r   r/   )r@   Z_ScreenrA   rc   rd   ZScrolledCanvasrb   ZadjustScrollsZ_rootwindowrw   rf   ZScreenÚscreenÚTurtleScreenra   ÚscanvasZ	RawTurtleZscreens)r_   r?   ZcanvasZ_s_r"   r"   r#   rO   è   s     

zDemoWindow.makeGraphFramec             C   s(   |t d< tt ƒ| jd< d| | jd< d S )Nr	   r9   zFont size %dr8   )rt   rs   r8   rP   )r_   Úsizer"   r"   r#   Úset_txtsizeø   s    zDemoWindow.set_txtsizec             C   s   |   ttd d tƒ¡ dS )Nr	   Úbreak)r}   Úmaxrt   ÚMINIMUM_FONT_SIZE)r_   Údummyr"   r"   r#   ru   ý   s    zDemoWindow.decrease_sizec             C   s   |   ttd d tƒ¡ dS )Nr	   r~   )r}   Úminrt   ÚMAXIMUM_FONT_SIZE)r_   r   r"   r"   r#   rv     s    zDemoWindow.increase_sizec             C   s$   |j dk t kr|  ¡ S |  ¡ S d S )Nr   )Zdeltar   ru   rv   )r_   re   r"   r"   r#   rx     s    zDemoWindow.update_mousewheelÚ Úbluec             C   sh   | j j||tkrdndd | jj||tkr0dndd | jj||tkrLdndd | jj||d d S )Nz#d00z#fca)r^   r4   )r8   r:   )rS   ÚconfigÚNORMALrU   rW   rP   )r_   ÚstartÚstopÚclearZtxtZcolorr"   r"   r#   r[     s    zDemoWindow.configGUIc                s>   t |ƒ}x0tƒ D ]&}|f‡ fdd„	}|j|dt|d qW |S )Nc                s   ˆ   | ¡ d S )N)rZ   )r!   )r_   r"   r#   Úload  s    z)DemoWindow.makeLoadDemoMenu.<locals>.loadr   )r1   r2   r9   r;   )rH   r(   Úadd_commandÚmenufont)r_   Úmasterr0   r!   r‹   r"   )r_   r#   rI     s    zDemoWindow.makeLoadDemoMenuc                sl   t |ƒ}|jdˆ jtd |jdˆ jtd | ¡  x2tD ]*}|f‡ fdd„	}|jt|ƒdt|d q:W |S )NzDecrease (C-'-'))r1   r;   r9   zIncrease (C-'+')c                s   ˆ   | ¡ d S )N)r}   )r|   )r_   r"   r#   Úresize)  s    z'DemoWindow.makeFontMenu.<locals>.resizer   )r1   r2   r9   r;   )rH   rŒ   ru   r   rv   Zadd_separatorÚ
font_sizesÚstr)r_   rŽ   r0   r|   r   r"   )r_   r#   rJ      s    


zDemoWindow.makeFontMenuc                s@   t |ƒ}x2tD ]*\}}||f‡ fdd„	}|j|t|d qW |S )Nc                s   t ˆ j| |ƒ d S )N)r   r?   )Ú
help_labelÚ	help_file)r_   r"   r#   Úshow3  s    z%DemoWindow.makeHelpMenu.<locals>.show)r1   r9   r;   )rH   Úhelp_entriesrŒ   r   )r_   rŽ   r0   r’   r“   r”   r"   )r_   r#   rK   /  s
    zDemoWindow.makeHelpMenuc             C   s   | j r| j ¡  d| _ d S )NF)rX   ry   rŠ   )r_   r"   r"   r#   ÚrefreshCanvas8  s    
zDemoWindow.refreshCanvasc          	   C   s–   |   ¡  dtj_d| }t|ƒ tj| | _t| jj	dƒ}| 
¡ }W d Q R X | j dd¡ | j d|¡ | j |d ¡ |  tttdd¡ t| _d S )	NFzturtledemo.Úrz1.0Úendz# - a Python turtle graphics examplezPress start buttonÚred)rV   r@   rz   Ú_RUNNINGÚ
__import__ÚsysÚmodulesÚmoduleÚopenÚ__file__Úreadr8   ÚdeleteÚinsertr?   rB   r[   r‡   r\   ÚREADYr^   )r_   r`   ÚmodnameÚfÚcharsr"   r"   r#   rZ   =  s    
zDemoWindow.loadfilec             C   sÚ   |   ¡  d| _dtj_|  tttdd¡ | j 	¡  | j 
d¡ t| _y$| j ¡ }|dkr`t| _nt| _W n. tjk
r–   | jd krˆd S t| _d}Y nX | jtkr´|  ttt|¡ n"| jtkrÖd| _|  tttdd¡ d S )	NTzdemo running...r>   ZstandardZ	EVENTLOOPzstopped!zuse mouse/keys or STOPr™   )r–   rX   r@   rz   rš   r[   r\   r‡   ry   rŠ   ÚmodeÚRUNNINGr^   rž   ÚmainÚEVENTDRIVENÚDONEZ
Terminatorr?   rY   )r_   Úresultr"   r"   r#   rR   L  s2    









zDemoWindow.startDemoc             C   s4   |   ¡  | j d¡ | jjdd |  ttt¡ d S )NÚallr„   )Zcursor)r–   ry   Z_deleter{   r†   r[   r‡   r\   )r_   r"   r"   r#   rV   i  s    zDemoWindow.clearCanvasc             C   s2   | j r&|  ¡  d| _ |  tttdd¡ dtj_d S )NFzSTOPPED!r™   )rY   rV   r[   r‡   r\   r@   rz   rš   )r_   r"   r"   r#   rT   o  s    
zDemoWindow.stopItc             C   s   dt j_| j ¡  d | _d S )NF)r@   rz   rš   r?   Zdestroy)r_   r"   r"   r#   rC   w  s    
zDemoWindow._destroy)N)N)N)r„   r…   )Ú__name__Ú
__module__Ú__qualname__ra   rf   rN   rO   r}   ru   rv   rx   r[   rI   rJ   rK   r–   rZ   rR   rV   rT   rC   r"   r"   r"   r#   r)   €   s$   
D


	
	r)   c              C   s   t ƒ } | j ¡  d S )N)r)   r?   Zmainloop)Zdemor"   r"   r#   rª   }  s    rª   Ú__main__)'r   rœ   r%   ZtkinterZidlelib.colorizerr   r   Zidlelib.percolatorr   Zidlelib.textviewr   Z
turtledemoZabout_turtledemor@   ÚpathÚdirnameÚabspathr    r'   Úplatformr   r]   r¤   r©   r¬   r«   r‡   r   rQ   rt   r€   rƒ   r   r(   r•   Úobjectr)   rª   r¯   r"   r"   r"   r#   Ú<module>W   s<   


 ~
