from Tkinter import Tk # from tkinter import Tk for Python 3.x
from tkinter.filedialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
from os.path import join,isdir
from pathlib import Path
from enquiries import choose,confirm
def dir_chooser(c_dir=getcwd(),selected_dirs=None,multiple=True) :
'''
This function shows a file chooser to select single or
multiple directories.
'''
selected_dirs = selected_dirs if selected_dirs else set([])
dirs = { item for item in listdir(c_dir) if isdir(join(c_dir, item)) }
dirs = { item for item in dirs if join(c_dir,item) not in selected_dirs and item[0] != "." } # Remove item[0] != "." if you want to show hidde
options = [ "Select This directory" ]
options.extend(dirs)
options.append("⬅")
info = f"You have selected : \n {','.join(selected_dirs)} \n" if len(selected_dirs) > 0 else "\n"
choise = choose(f"{info}You are in {c_dir}", options)
if choise == options[0] :
selected_dirs.add(c_dir)
if multiple and confirm("Do you want to select more folders?") :
return get_folders(Path(c_dir).parent,selected_dirs,multiple)
return selected_dirs
if choise == options[-1] :
return get_folders(Path(c_dir).parent,selected_dirs,multiple)
return get_folders(join(c_dir,choise),selected_dirs,multiple)