
    3f[:                        d Z g dZddlZddlZddlmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ i Z G d d	      Z G d
 de      Z G d de      Z G d dej,                        Z G d de      Z G d de      Z  G d dej,                        Z!d Z"d Z#d Z$ddZ%ddZ&d dZ'd Z(d Z)e*dk(  r e)        yy)!aU  File selection dialog classes.

Classes:

- FileDialog
- LoadFileDialog
- SaveFileDialog

This module also presents tk common file dialogues, it provides interfaces
to the native file dialogues available in Tk 4.2 and newer, and the
directory dialogue available in Tk 8.3 and newer.
These interfaces were written by Fredrik Lundh, May 1997.
)
FileDialogLoadFileDialogSaveFileDialogOpenSaveAs	Directoryaskopenfilenameasksaveasfilenameaskopenfilenamesaskopenfileaskopenfilesasksaveasfileaskdirectory    N)FrameLEFTYESBOTTOMEntryTOPButtonTkXToplevelRIGHTYENDListboxBOTH	Scrollbar)Dialog)commondialog)_setup_dialogc                       e Zd ZdZdZddZej                  dddfdZddZ	d	 Z
d
 Zd Zd Zd Zd ZddZd Zd ZddZd Zd Zy)r   a  Standard file selection dialog -- no checks on selected file.

    Usage:

        d = FileDialog(master)
        fname = d.go(dir_or_file, pattern, default, key)
        if fname is None: ...canceled...
        else: ...open file...

    All arguments to go() are optional.

    The 'key' argument specifies a key in the global dictionary
    'dialogstates', which keeps track of the values for the directory
    and pattern arguments, overriding the values passed in (it does
    not keep track of the default argument!).  If no key is specified,
    the dialog keeps no memory of previous state.  Note that memory is
    kept even when the dialog is canceled.  (All this emulates the
    behavior of the Macintosh file selection dialogs.)

    zFile Selection DialogNc                 *   || j                   }|| _        d | _        t        |      | _        | j                  j                  |       | j                  j                  |       t        | j                         t        | j                        | _        | j                  j                  t        t               t        | j                        | _        | j                  j                  t        t               | j                  j                  d| j                         t        | j                        | _        | j                   j                  t"        t               | j                   j                  d| j$                         t        | j                        | _        | j&                  j                  t(        t*               t-        | j&                        | _        | j.                  j                  t0        t2               t5        | j&                  d| j.                  df      | _        | j6                  j                  t0        t(        t*               | j6                  j9                         }| j6                  j9                  |dd  |d d z          | j6                  j                  d	| j:                         | j6                  j                  d
| j<                         | j.                  j?                  | j6                  df       t-        | j&                        | _         | j@                  j                  tB        t2               t5        | j&                  d| j@                  df      | _"        | jD                  j                  tB        t(        t*               | j@                  j?                  | jD                  df       | jD                  j9                         }| jD                  j9                  |dd  |d d z          | jD                  j                  d	| jF                         | jD                  j                  d
| jH                         tK        | j                  d| jL                        | _'        | jN                  j                  tB               tK        | j                  d| j$                        | _(        | jP                  j                  tB        t(               tK        | j                  d| jR                        | _*        | jT                  j                  t0               | j                  jW                  d| jR                         | j                  j                  d| jR                         | j                  j                  d| jR                         y )N)sidefillz<Return>)expandr&   r   set)exportselectionyscrollcommand)r%   r'   r&      z<ButtonRelease-1>z<Double-ButtonRelease-1>yview)commandOK)textr-   )r%   Filter)r%   r'   CancelWM_DELETE_WINDOWz<Alt-w>z<Alt-W>),titlemaster	directoryr   topiconnamer"   r   botframepackr   r   r   	selectionbindok_eventfilterr   filter_commandmidframer   r   r   filesbarr   r   r   filesbindtagsfiles_select_eventfiles_double_eventconfigdirsbarr   dirsdirs_select_eventdirs_double_eventr   
ok_command	ok_buttonfilter_buttoncancel_commandcancel_buttonprotocol)selfr4   r3   btagss       )/usr/lib/python3.12/tkinter/filedialog.py__init__zFileDialog.__init__:   s   =$**%F#u% dhhdhhQ/txxa0J6DHHoc*T%8%89dhh#D1!$--0A.T]]A-1]]E,BD


U3T:

##%

E!"Ibq	12

+T-D-DE

2D4K4KLdjj'%:; /t!,DMM1,0LL%+@B			D48TYY$89		""$		59uRay01		*D,B,BC		143I3IJ&*)-: 	&#DMM)1,0,?,?A 	T#6#DMM)1,0,?,?A 	U+,d.A.ABi!4!45i!4!45    * c                 b   |r|t         v rt         |   \  | _        }nmt        j                  j	                  |      }t        j                  j                  |      r|| _        n't        j                  j                  |      \  | _        }| j                  | j                  |       | j                  |       | j                          | j                  j                          | j                  j                          | j                  j                          d | _        | j                   j#                          |rS| j%                         \  }}| j                  r)t        j                  j'                  | j                        }||ft         |<   | j                  j)                          | j                  S N)dialogstatesr5   ospath
expanduserisdirsplit
set_filterset_selectionr>   r:   	focus_setr6   wait_visibilitygrab_sethowr4   mainloop
get_filterdirnamedestroy)rP   dir_or_filepatterndefaultkeyr5   s         rR   gozFileDialog.goz   s-   3,&&23&7#DNG'',,[9Kww}}[)!,*,''--*D'07#  "  "!%!2IwxxGGOODHH5	 )7 2LxxrT   c                 F    || _         | j                  j                          y rX   )rd   r4   quit)rP   rd   s     rR   ro   zFileDialog.quit   s    rT   c                 $    | j                          y rX   )r>   rP   events     rR   rI   zFileDialog.dirs_double_event   s    rT   c                    | j                         \  }}| j                  j                  d      }t        j                  j                  t        j                  j                  | j                  |            }| j                  ||       y Nactive)	rf   rG   getrZ   r[   normpathjoinr5   r_   )rP   rr   dirpatsubdirs        rR   rH   zFileDialog.dirs_select_event   sY    ??$Sx(ggrww||DNNFCDS!rT   c                 $    | j                          y rX   rJ   rq   s     rR   rD   zFileDialog.files_double_event       rT   c                 \    | j                   j                  d      }| j                  |       y rt   )rA   rv   r`   )rP   rr   files      rR   rC   zFileDialog.files_select_event   s"    zz~~h'4 rT   c                 $    | j                          y rX   r}   rq   s     rR   r<   zFileDialog.ok_event   r~   rT   c                 B    | j                  | j                                y rX   )ro   get_selectionrP   s    rR   rJ   zFileDialog.ok_command   s    		$$$&'rT   c                    | j                         \  }}	 t        j                  |      }|| _        | j                  ||       |j                          t        j                  g}g }|D ]{  }t        j                  j                  ||      }t        j                  j                  |      r|j                  |       Tt        j                  ||      sk|j                  |       } | j                  j!                  dt"               |D ]"  }| j                  j%                  t"        |       $ | j&                  j!                  dt"               |D ]"  }| j&                  j%                  t"        |       $ t        j                  j)                  | j+                               \  }	}
|
t        j,                  k(  rd}
| j/                  |
       y # t        $ r | j                  j                          Y y w xY w)Nr   rV   )rf   rZ   listdirOSErrorr4   bellr5   r_   sortpardirr[   rx   r]   appendfnmatchrG   deleter   insertrA   r^   r   curdirr`   )rP   rr   ry   rz   namessubdirsmatchingfilesnamefullnameheadtails              rR   r>   zFileDialog.filter_command   s   ??$S	JJsOE S!

99+ 	+Dww||C.Hww}}X&t$s+$$T*	+ 			C  	(DIIS$'	(

!S!! 	)DJJc4(	)WW]]4#5#5#78
d299Rd4 -  	KK	s   G #G10G1c                 \   | j                   j                         }t        j                  j	                  |      }|dd  t        j
                  k(  st        j                  j                  |      r t        j                  j                  |d      }t        j                  j                  |      S )NrU   )	r=   rv   rZ   r[   r\   sepr]   rx   r^   )rP   r=   s     rR   rf   zFileDialog.get_filter   sn    "##F+"#;"&& BGGMM&$9WW\\&#.Fww}}V$$rT   c                 x    | j                   j                         }t        j                  j	                  |      }|S rX   )r:   rv   rZ   r[   r\   rP   r   s     rR   r   zFileDialog.get_selection   s-    ~~!!#ww!!$'rT   c                 $    | j                          y rX   )ro   rq   s     rR   rM   zFileDialog.cancel_command   s    		rT   c                    t         j                  j                  |      sV	 t        j                         }|r?t         j                  j                  ||      }t         j                  j                  |      }| j                  j                  dt               | j                  j                  t        t         j                  j                  |xs t         j                  |xs d             y # t        $ r d }Y w xY w)Nr   rU   )rZ   r[   isabsgetcwdr   rx   rw   r=   r   r   r   r   )rP   ry   rz   pwds       rR   r_   zFileDialog.set_filter   s    ww}}S!iik ggll3,gg&&s+1c"3S-=BIIszc JK  s   C+ +C98C9c                     | j                   j                  dt               | j                   j                  t        t        j
                  j                  | j                  |             y )Nr   )r:   r   r   r   rZ   r[   rx   r5   r   s     rR   r`   zFileDialog.set_selection   s=    a%c277<<#EFrT   rX   )__name__
__module____qualname____doc__r3   rS   rZ   r   rm   ro   rI   rH   rD   rC   r<   rJ   r>   rf   r   rM   r_   r`    rT   rR   r   r   !   sk    * $E>6@  YYRT 2"!(!8%

LGrT   r   c                       e Zd ZdZdZd Zy)r   z8File selection dialog which checks that the file exists.zLoad File Selection Dialogc                     | j                         }t        j                  j                  |      s| j                  j                          y | j                  |       y rX   )r   rZ   r[   isfiler4   r   ro   r   s     rR   rJ   zLoadFileDialog.ok_command   s;    !!#ww~~d#KKIIdOrT   Nr   r   r   r   r3   rJ   r   rT   rR   r   r      s    B(ErT   r   c                       e Zd ZdZdZd Zy)r   z@File selection dialog which checks that the file may be created.zSave File Selection Dialogc                    | j                         }t        j                  j                  |      rit        j                  j	                  |      r| j
                  j                          y t        | j                  dd|dddd      }|j                  dk7  r]y t        j                  j                  |      \  }}t        j                  j	                  |      s| j
                  j                          y | j                  |       y )	Nz Overwrite Existing File QuestionzOverwrite existing file ?	questheadr+   )Yesr1   )r3   r/   bitmaprk   stringsr   )r   rZ   r[   existsr]   r4   r   r    r6   numr^   ro   )rP   r   dr   r   s        rR   rJ   zSaveFileDialog.ok_command   s    !!#77>>$ww}}T"  "txx?=AC) 02A uuzt,JD$77==&  "		$rT   Nr   r   rT   rR   r   r      s    J(ErT   r   c                       e Zd Zd Zd Zy)_Dialogc                 n    	 t        | j                  d         | j                  d<   y # t        $ r Y y w xY w)N	filetypes)tupleoptionsKeyErrorr   s    rR   _fixoptionsz_Dialog._fixoptions2  s5    	(-dll;.G(HDLL% 		s   %( 	44c                     |rM	 |j                   }t        j                  j	                  |      \  }}|| j
                  d<   || j
                  d<   || _        |S # t        $ r Y Tw xY w)N
initialdirinitialfile)stringAttributeErrorrZ   r[   r^   r   filename)rP   widgetresultr[   r   s        rR   
_fixresultz_Dialog._fixresult9  sg     v.JD$)-DLL&*.DLL' " s   A 	A%$A%N)r   r   r   r   r   r   rT   rR   r   r   0  s    rT   r   c                       e Zd ZdZdZd Zy)r   Ask for a filename to opentk_getOpenFilec           
         t        |t              r\t        |D cg c]  }t        |d|       c}      }|r4t        j                  j                  |d         \  }}|| j                  d<   |S |j                  j                         s9d| j                  v r+| j                  ||j                  j                  |            S t        j                  | ||      S c c}w )Nr   r   r   multiple)
isinstancer   getattrrZ   r[   r^   r   tkwantobjectsr   	splitlistr   )rP   r   r   rr[   r   s         rR   r   zOpen._fixresultQ  s    fe$VDGAx3DEFWW]]6!95
d-1\*Myy$$&:+E??6699+>+>v+FGG!!$77 Es   CNr   r   r   r   r-   r   r   rT   rR   r   r   L  s     G8rT   r   c                       e Zd ZdZdZy)r   Ask for a filename to save astk_getSaveFileN)r   r   r   r   r-   r   rT   rR   r   r   `  s
    #GrT   r   c                       e Zd ZdZdZd Zy)r   zAsk for a directorytk_chooseDirectoryc                 n    |r	 |j                   }|| j                  d<   || _        |S # t        $ r Y #w xY w)Nr   )r   r   r   r5   )rP   r   r   s      rR   r   zDirectory._fixresultl  sC    
 *0DLL& " s   ( 	44Nr   r   rT   rR   r   r   g  s    "GrT   r   c                  4    t        di | j                         S )r   r   r   showr   s    rR   r   r   }  s     ?'?!!rT   c                  4    t        di | j                         S )r   r   )r   r   r   s    rR   r	   r	     s     G!!##rT   c                  >    d| d<   t        di | j                         S )ztAsk for multiple filenames to open

    Returns a list of filenames or empty list if
    cancel button selected
    r+   r   r   r   r   s    rR   r
   r
     s#     GJ?'?!!rT   c                 R    t        di |j                         }|rt        ||       S y)z8Ask for a filename to open, and returned the opened fileNr   )r   r   openmoder   r   s      rR   r   r     s+     g##%HHd##rT   c                 l    t        di |}|r&g }|D ]  }|j                  t        ||               |}|S )zAsk for multiple filenames and return the open file
    objects

    returns a list of open file objects or an empty list if
    cancel selected
    r   )r
   r   r   )r   r   rA   ofilesr   s        rR   r   r     sE     'w'E 	0HMM$x./	0LrT   c                 R    t        di |j                         }|rt        ||       S y)z;Ask for a filename to save as, and returned the opened fileNr   )r   r   r   r   s      rR   r   r     s-       %%'HHd##rT   c                  4    t        di | j                         S )z-Ask for a directory, and return the file namer   )r   r   r   s    rR   r   r     s    w$$&&rT   c                     t               } | j                          t        |       }|j                  d      }t	        |       }|j                  d      }t        ||       d}	 ddl}|j                  |j                  d       |j                  |j                        }t        dg      }	 t        |d	      }|j                          t        d|j#                  |             t%               }	t        d|	j#                  |             y# t        t        f$ r Y |w xY w# t         $ r }t        d
       t        |       Y d}~zd}~ww xY w)zSimple test program.test)rl   zutf-8r   NrV   )z	all filesrU   )r   r   zCould not open File: r   saveas)r   withdrawr   rm   r   printlocale	setlocaleLC_ALLnl_langinfoCODESETImportErrorr   r   r   closeBaseExceptionencoder	   )
rootfdloadfilesavefileencr   openfilenamefpexcsaveasfilenames
             rR   r   r     s#   4DMMO		Buuu H		Buuu H	(H Cr*  0 !,>+?@LS!


 
&,%%c*+ %&N	(N))#./% (   %&c

s*   $;D
 -D 
DD	E(EE__main__)r   )w)+r   __all__r   rZ   tkinterr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   tkinter.dialogr    r!   tkinter.simpledialogr"   rY   r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   rT   rR   <module>r     s   K
  	     "   . EG EGPZ Z rl!! 887 8(W ## ,"$""'(0V zF rT   