a
    Ö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¡ tt|ƒƒD ]L}|| }	t| j	|	| |fdd„d	}
||krÜ|
jtd
d |
jttdd q | j d| j¡ |  |¡ d S )N)Úclass_i  )ÚtextZaspecté   )ÚexpandÚfillú<Return>c                 S   s
   |   |¡S ©N)Údone©ÚselfÚnum© r   ú%lib/python3.9/tkinter/simpledialog.pyÚ<lambda>7   ó    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__"   s0    

ÿ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   ú+%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 r   )r   Úwait_visibilityÚgrab_setÚmainloopÚdestroyr   ©r   r   r   r   Úgo[   s
    



zSimpleDialog.goc                 C   s&   | j d u r| j ¡  n|  | j ¡ d S r   )r"   r   Úbellr   ©r   Zeventr   r   r   r$   b   s    
zSimpleDialog.return_eventc                 C   s&   | j d u r| j ¡  n|  | j ¡ d S r   )r!   r   rC   r   rA   r   r   r   r*   h   s    
zSimpleDialog.wm_delete_windowc                 C   s   || _ | j ¡  d S r   )r   r   Úquitr   r   r   r   r   n   s    zSimpleDialog.done)r1   r2   )	Ú__name__Ú
__module__Ú__qualname__r0   r+   rB   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u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   r3   é2   )r   r0   r4   Zwinfo_viewabler5   r   ÚparentÚresultr   ÚbodyÚinitial_focusr   Ú	buttonboxr)   r!   r9   r7   r8   r:   Ú	focus_setr=   r>   Zwait_window)r   rN   r   rP   r   r   r   r0   z   s0    	



ÿ
zDialog.__init__c                 C   s   d| _ t | ¡ dS )zDestroy the windowN)rQ   r   r@   rA   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   rP   ³   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"   rJ   )r   rK   rL   ÚCancel)r   rU   r   r   z<Escape>N)r   r'   ÚokZACTIVEr   r(   r!   r#   )r   ZboxÚwr   r   r   rR   ¼   s    zDialog.buttonboxc                 C   sJ   |   ¡ s| j ¡  d S |  ¡  |  ¡  z|  ¡  W |  ¡  n
|  ¡  0 d S r   )ÚvalidaterQ   rS   r4   r6   Úapplyr!   rD   r   r   r   rW   Ñ   s    

z	Dialog.okc                 C   s    | j d ur| j  ¡  |  ¡  d S r   )rN   rS   r@   rD   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   rA   r   r   r   rY   é   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   rA   r   r   r   rZ   ò   s    zDialog.apply)N)N)N)rF   rG   rH   Ú__doc__r0   r@   rP   rR   rW   r!   rY   rZ   r   r   r   r   rI   s   s   
1	


	rI   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 r   )ÚtkinterZ_default_rootÚpromptÚminvalueÚmaxvalueÚinitialvaluerI   r0   )r   r   r^   ra   r_   r`   rN   r   r   r   r0     s    z_QueryDialog.__init__c                 C   s   d | _ t | ¡ d S r   )ÚentryrI   r@   rA   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 url| j 	d| j¡ | j 
dt¡ | jS )N)r   Zjustifyr   rJ   )ÚrowrK   Zstickyrb   )Únamer   )ZLabelr^   r(   ZgridÚWZEntryrb   ÚEra   ÚinsertZselect_rangeZEND)r   r,   rX   r   r   r   rP     s    
z_QueryDialog.bodyc                 C   sž   z|   ¡ }W n* ty6   tjd| jd | d Y dS 0 | jd urf|| jk rftjdd| j | d dS | jd ur”|| jkr”tjdd| j | d dS || _d	S )
NzIllegal valuez
Please try again)rN   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Úerrormessager_   r`   rO   )r   rO   r   r   r   rY   #  s:    ýÿüÿüz_QueryDialog.validate)NNNN)rF   rG   rH   r0   r@   rP   rY   r   r   r   r   r\   ÿ   s      ý
r\   c                   @   s   e Zd ZdZdd„ ZdS )Ú_QueryIntegerzNot an integer.c                 C   s   |   | j ¡ ¡S r   )Zgetintrb   ÚgetrA   r   r   r   rh   H  s    z_QueryInteger.getresultN©rF   rG   rH   rk   rh   r   r   r   r   rl   E  s   rl   c                 K   s   t | |fi |¤Ž}|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
    )rl   rO   ©r   r^   ÚkwÚdr   r   r   Ú
askintegerL  s    rr   c                   @   s   e Zd ZdZdd„ ZdS )Ú_QueryFloatzNot a floating point value.c                 C   s   |   | j ¡ ¡S r   )Z	getdoublerb   rm   rA   r   r   r   rh   ^  s    z_QueryFloat.getresultNrn   r   r   r   r   rs   [  s   rs   c                 K   s   t | |fi |¤Ž}|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
    )rs   rO   ro   r   r   r   Úaskfloatb  s    rt   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú_QueryStringc                 O   s<   d|v r|d | _ |d= nd | _ tj| g|¢R i |¤Ž d S )NÚshow)Ú_QueryString__showr\   r0   )r   Úargsrp   r   r   r   r0   r  s
    
z_QueryString.__init__c                 C   s(   t  | |¡}| jd ur$|j| jd |S )N)rv   )r\   rP   rw   Z	configure)r   r,   rb   r   r   r   rP   z  s    
z_QueryString.bodyc                 C   s
   | j  ¡ S r   )rb   rm   rA   r   r   r   rh   €  s    z_QueryString.getresultN)rF   rG   rH   r0   rP   rh   r   r   r   r   ru   q  s   ru   c                 K   s   t | |fi |¤Ž}|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
    )ru   rO   ro   r   r   r   Ú	askstring„  s    ry   Ú__main__c                  C   sL   t ƒ } | fdd„}t| d|d}| ¡  t| d|jd}| ¡  | ¡  d S )Nc                 S   s\   t | dg d¢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NorV   r   é   zTest Dialog)r   r-   r"   r!   r   ZSpamz	Egg counté   )ra   zEgg weight
(in tons)r   éd   )r_   r`   z	Egg label)r   ÚprintrB   rr   rt   ry   )r   rq   r   r   r   Údoit—  s    ÷

ÿztest.<locals>.doitZTestr   ZQuit)ZTkr'   r   rE   r?   )r   r   ÚtÚqr   r   r   Útest•  s    r‚   )r[   r]   r   r   r   rI   r\   rl   rr   rs   rt   ru   ry   rF   r‚   r   r   r   r   Ú<module>   s    S F