B
    ݜwa,                 @   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rd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 qW | 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 rd}||
 |
 kr|
 |
 }n|dk rd}|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 )zInitialize a dialog.

        Arguments:

            parent -- a parent window (the application window)

            title -- the dialog title
        N   )padxpadyr   z+%d+%d2   )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 )zcreate 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 )zvalidate 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 )zprocess 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rt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 )zget 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 )zget 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 )zget 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 )NzThis 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