B
    ] Çc ,  ã               @   s°   d Z ddlT ddlmZ ddlZG dd„ dƒZG dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZdd„ ZG dd„ deƒZ	dd„ Z
G dd„ deƒZdd„ Zedkr¬dd„ Zeƒ  dS )a&  This modules handles dialog boxes.

It contains the following public symbols:

SimpleDialog -- A simple but flexible modal dialog box

Dialog -- a base class for dialogs

askinteger -- get an integer from the user

askfloat -- get a float from the user

askstring -- get a string from the user
é    )Ú*)Ú
messageboxNc               @   sL   e Zd Zdg ddddfdd„Zddd„Zd	d
„ Zdd„ Zdd„ Zdd„ ZdS )ÚSimpleDialogÚ Nc             C   s  |rt ||d| _n
t |ƒ| _|r:| j |¡ | j |¡ t| j|dd| _| jjdtd t| jƒ| _	| j	 ¡  || _
|| _|| _| j d| j¡ x\tt|ƒƒD ]L}|| }	t| j	|	| |fdd„d	}
||krÞ|
jtd
d |
jttdd q¢W | j d| j¡ |  |¡ d S )N)Úclass_i  )ÚtextZaspecté   )ÚexpandÚfillz<Return>c             S   s
   |   |¡S )N)Údone)ÚselfÚnum© r   ú%lib/python3.7/tkinter/simpledialog.pyÚ<lambda>6   ó    z'SimpleDialog.__init__.<locals>.<lambda>)r   Úcommandé   )ZreliefZborderwidth)Úsider
   r	   ÚWM_DELETE_WINDOW)ÚToplevelÚrootÚtitleZiconnameZMessageÚmessageÚpackZBOTHÚFrameÚframer   ÚcancelÚdefaultÚbindÚreturn_eventÚrangeÚlenÚButtonZconfigZRIDGEÚLEFTÚprotocolÚwm_delete_windowÚ_set_transient)r   Úmasterr   Úbuttonsr   r   r   r   r   ÚsÚbr   r   r   Ú__init__!   s.    

zSimpleDialog.__init__ç      à?ç333333Ó?c             C   s  | j }| ¡  | |¡ | ¡  | ¡ rJ| ¡ }| ¡ }| ¡ }| ¡ }n| 	¡ }| 
¡ }d }}| ¡ }	| ¡ }
|||	 |  }|||
 |  }||	 | 	¡ kr°| 	¡ |	 }n|dk r¼d}||
 | 
¡ krÚ| 
¡ |
 }n|dk ræd}| d||f ¡ | ¡  d S )Nr   z+%d+%d)r   ÚwithdrawÚ	transientÚupdate_idletasksZwinfo_ismappedZwinfo_widthZwinfo_heightÚwinfo_rootxÚwinfo_rootyZwinfo_screenwidthZwinfo_screenheightZwinfo_reqwidthZwinfo_reqheightÚgeometryÚ	deiconify)r   r(   ZrelxZrelyZwidgetZm_widthZm_heightZm_xZm_yZw_widthZw_heightÚxÚyr   r   r   r'   =   s4    

zSimpleDialog._set_transientc             C   s.   | j  ¡  | j  ¡  | j  ¡  | j  ¡  | jS )N)r   Úwait_visibilityÚgrab_setÚmainloopÚdestroyr   )r   r   r   r   ÚgoZ   s
    



zSimpleDialog.goc             C   s&   | j d kr| j ¡  n|  | j ¡ d S )N)r   r   Úbellr   )r   Úeventr   r   r   r    a   s    
zSimpleDialog.return_eventc             C   s&   | j d kr| j ¡  n|  | j ¡ d S )N)r   r   r=   r   )r   r   r   r   r&   g   s    
zSimpleDialog.wm_delete_windowc             C   s   || _ | j ¡  d S )N)r   r   Úquit)r   r   r   r   r   r   m   s    zSimpleDialog.done)r-   r.   )	Ú__name__Ú
__module__Ú__qualname__r,   r'   r<   r    r&   r   r   r   r   r   r      s   
r   c               @   sV   e Zd ZdZd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S )ÚDialogzZClass to open dialogs.

    This class is intended as a base class for custom dialogs
    Nc             C   sà   t  | |¡ |  ¡  | ¡ r&|  |¡ |r4|  |¡ || _d| _t| ƒ}|  	|¡| _
|jddd |  ¡  | j
sv| | _
|  d| j¡ | jdk	r°|  d| ¡ d | ¡ d f ¡ |  ¡  | j
 ¡  |  ¡  |  ¡  |  | ¡ dS )z˜Initialize a dialog.

        Arguments:

            parent -- a parent window (the application window)

            title -- the dialog title
        Né   )ÚpadxÚpadyr   z+%d+%dé2   )r   r,   r/   Zwinfo_viewabler0   r   ÚparentÚresultr   ÚbodyÚinitial_focusr   Ú	buttonboxr%   r   r4   r2   r3   r5   Ú	focus_setr8   r9   Zwait_window)r   rH   r   rJ   r   r   r   r,   y   s.    




zDialog.__init__c             C   s   d| _ t | ¡ dS )zDestroy the windowN)rK   r   r;   )r   r   r   r   r;   «   s    zDialog.destroyc             C   s   dS )z«create dialog body.

        return widget that should have initial focus.
        This method should be overridden, and is called
        by the __init__ method.
        Nr   )r   r(   r   r   r   rJ   ³   s    zDialog.bodyc             C   sv   t | ƒ}t|dd| jtd}|jtddd t|dd| jd}|jtddd |  d| j¡ |  d	| j¡ | ¡  d
S )z[add standard button box.

        override if you do not want the standard buttons
        ZOKé
   )r   Úwidthr   r   rD   )r   rE   rF   ÚCancel)r   rO   r   z<Return>z<Escape>N)r   r#   ÚokZACTIVEr   r$   r   r   )r   ZboxÚwr   r   r   rL   ¼   s    zDialog.buttonboxc             C   sB   |   ¡ s| j ¡  d S |  ¡  |  ¡  z|  ¡  W d |  ¡  X d S )N)ÚvalidaterK   rM   r/   r1   Úapplyr   )r   r>   r   r   r   rQ   Ñ   s    
z	Dialog.okc             C   s    | j d k	r| j  ¡  |  ¡  d S )N)rH   rM   r;   )r   r>   r   r   r   r   ß   s    

zDialog.cancelc             C   s   dS )z¨validate the data

        This method is called automatically to validate the data before the
        dialog is destroyed. By default, it always validates OK.
        r   r   )r   r   r   r   rS   é   s    zDialog.validatec             C   s   dS )z¡process the data

        This method is called automatically to process the data, *after*
        the dialog is destroyed. By default, it does nothing.
        Nr   )r   r   r   r   rT   ò   s    zDialog.apply)N)N)N)r@   rA   rB   Ú__doc__r,   r;   rJ   rL   rQ   r   rS   rT   r   r   r   r   rC   r   s   
2	


	rC   c               @   s.   e Zd Zd
dd„Zdd„ Zdd„ Zdd	„ ZdS )Ú_QueryDialogNc             C   s4   |s
t j}|| _|| _|| _|| _t | ||¡ d S )N)ÚtkinterZ_default_rootÚpromptÚminvalueÚmaxvalueÚinitialvaluerC   r,   )r   r   rX   r[   rY   rZ   rH   r   r   r   r,     s    z_QueryDialog.__init__c             C   s   d | _ t | ¡ d S )N)ÚentryrC   r;   )r   r   r   r   r;     s    z_QueryDialog.destroyc             C   sr   t || jtd}|jddtd t|dd| _| jjddtt d | jd k	rl| j 	d| j¡ | j 
dt¡ | jS )N)r   Zjustifyr   rD   )ÚrowrE   Zstickyr\   )Únamer   )ZLabelrX   r$   ZgridÚWZEntryr\   ÚEr[   ÚinsertZselect_rangeZEND)r   r(   rR   r   r   r   rJ     s    
z_QueryDialog.bodyc             C   sž   y|   ¡ }W n* tk
r6   tjd| jd | d dS X | jd k	rf|| jk rftjdd| j | d dS | jd k	r”|| jkr”tjdd| j | d dS || _d	S )
NzIllegal valuez
Please try again)rH   r   z	Too smallz2The allowed minimum value is %s. Please try again.z	Too largez2The allowed maximum value is %s. Please try again.r   )Ú	getresultÚ
ValueErrorr   ÚshowwarningÚerrormessagerY   rZ   rI   )r   rI   r   r   r   rS   #  s0    z_QueryDialog.validate)NNNN)r@   rA   rB   r,   r;   rJ   rS   r   r   r   r   rV   ÿ   s     
rV   c               @   s   e Zd ZdZdd„ ZdS )Ú_QueryIntegerzNot an integer.c             C   s   |   | j ¡ ¡S )N)Zgetintr\   Úget)r   r   r   r   rb   G  s    z_QueryInteger.getresultN)r@   rA   rB   re   rb   r   r   r   r   rf   E  s   rf   c             K   s   t | |f|Ž}|jS )z¼get an integer from the user

    Arguments:

        title -- the dialog title
        prompt -- the label text
        **kw -- see SimpleDialog class

    Return value is an integer
    )rf   rI   )r   rX   ÚkwÚdr   r   r   Ú
askintegerJ  s    rj   c               @   s   e Zd ZdZdd„ ZdS )Ú_QueryFloatzNot a floating point value.c             C   s   |   | j ¡ ¡S )N)Z	getdoubler\   rg   )r   r   r   r   rb   Z  s    z_QueryFloat.getresultN)r@   rA   rB   re   rb   r   r   r   r   rk   X  s   rk   c             K   s   t | |f|Ž}|jS )z¶get a float from the user

    Arguments:

        title -- the dialog title
        prompt -- the label text
        **kw -- see SimpleDialog class

    Return value is a float
    )rk   rI   )r   rX   rh   ri   r   r   r   Úaskfloat]  s    rl   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú_QueryStringc             O   s6   d|kr|d | _ |d= nd | _ tj| f|ž|Ž d S )NÚshow)Ú_QueryString__showrV   r,   )r   Úargsrh   r   r   r   r,   l  s
    
z_QueryString.__init__c             C   s(   t  | |¡}| jd k	r$|j| jd |S )N)rn   )rV   rJ   ro   Z	configure)r   r(   r\   r   r   r   rJ   t  s    
z_QueryString.bodyc             C   s
   | j  ¡ S )N)r\   rg   )r   r   r   r   rb   z  s    z_QueryString.getresultN)r@   rA   rB   r,   rJ   rb   r   r   r   r   rm   k  s   rm   c             K   s   t | |f|Ž}|jS )z¸get a string from the user

    Arguments:

        title -- the dialog title
        prompt -- the label text
        **kw -- see SimpleDialog class

    Return value is a string
    )rm   rI   )r   rX   rh   ri   r   r   r   Ú	askstring}  s    rq   Ú__main__c              C   sL   t ƒ } | fdd„}t| d|d}| ¡  t| d|jd}| ¡  | ¡  d S )Nc             S   s^   t | ddddgdddd}t| ¡ ƒ ttd	d
ddƒ ttd	ddddƒ ttd	dƒƒ d S )Nz‘This is a test dialog.  Would this have been an actual dialog, the buttons below would have been glowing in soft pink light.
Do you believe this?ZYesZNorP   r   é   zTest Dialog)r   r)   r   r   r   ZSpamz	Egg counté   )r[   zEgg weight
(in tons)r   éd   )rY   rZ   z	Egg label)r   Úprintr<   rj   rl   rq   )r   ri   r   r   r   Údoit‘  s    

ztest.<locals>.doitZTest)r   r   ZQuit)ZTkr#   r   r?   r:   )r   rw   ÚtÚqr   r   r   Útest  s    rz   )rU   rW   r   r   r   rC   rV   rf   rj   rk   rl   rm   rq   r@   rz   r   r   r   r   Ú<module>   s    S F