1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import os
from win32com.shell import shell,shellcon
import win32gui
class FolderBrowser():
def __init__(self, text='', initialFolder=os.getcwd()):
#variables :
self.title=text
self.initialFolder=initialFolder
# A callback procedure - called by SHBrowseForFolder
def BrowseCallbackProc(self, hwnd, msg, lp, data):
if msg== shellcon.BFFM_INITIALIZED:
win32gui.SendMessage(hwnd, shellcon.BFFM_SETSELECTION, 1, data)
elif msg == shellcon.BFFM_SELCHANGED:
# Set the status text of the
# For this message, 'lp' is the address of the PIDL.
pidl = shell.AddressAsPIDL(lp)
try:
path = shell.SHGetPathFromIDList(pidl)
win32gui.SendMessage(hwnd, shellcon.BFFM_SETSTATUSTEXT, 0, path)
except shell.error:
# No path for this PIDL
pass
def selectFolder(self):
#A dialog with the self.itialFolder selected as the default - this
# must be done via a callback function.
pidl=shell.SHBrowseForFolder(0, # parent HWND
None, # root PIDL.
self.title, # title
0x0040 | 0x0010, # flags: use new window design (ajustable size)
self.BrowseCallbackProc, # callback function
self.initialFolder # 'data' param for the callback
)
return shell.SHGetPathFromIDList (pidl[0])
#Test :
selectedFolder=FolderBrowser().selectFolder()
print selectedFolder |
Partager