Browse dialog in PythonWin
Roger Burnham
rburnham at cri-inc.com
Wed Apr 19 18:38:07 EDT 2000
On Wed, 19 Apr 2000 16:26:51 -0600, Ivan Van Laningham <ivanlan at callware.com> wrote:
>Hi All--
>OK, I've been able to get my dialogs up and even install handlers for
>button clicks, but now I need to start up a browse dialog that lets
>users choose directories.
>
>And is there any news on Mark Hammond's Starship page?
>
><not-that-i'm-frustrated-but-my-keyboard-has-teeth-marks-now>-ly y'rs,
>Ivan
>----------------------------------------------
>Ivan Van Laningham
>Callware Technologies, Inc.
>http://www.pauahtun.org
>http://www.foretec.com/python/workshops/1998-11/proceedings.html
>Army Signal Corps: Cu Chi, Class of '70
>Author: Teach Yourself Python in 24 Hours
>
Ivan,
Here's a dialog I adapted from something I found in the PythonWin
distribution.
-------------------------------------------------------------------
import sys, os
import win32ui, win32api, win32con, win32file
import string
from pywin.tools import hierlist
import commctrl
def dirParts(path):
parts = []
drive, path = os.path.splitdrive(path)
pp = os.path.split(path)
parts.append(pp[-1])
while pp[0] != path:
path = pp[0]
pp = os.path.split(path)
if pp[-1]:
parts.append(pp[-1])
parts.append(drive+'\\')
parts.reverse()
return parts
class DirHierList(hierlist.HierList):
def __init__(self, root, listBoxID=win32ui.IDC_LIST1, defaultDir=None):
self.defDir = defaultDir
hierlist.HierList.__init__(self, root,
win32ui.IDB_HIERFOLDERS, listBoxID)
self.choice = None
def HierInit(self, parent, listControl=None ):
hierlist.HierList.HierInit(self, parent, listControl)
if self.defDir is not None:
parts = dirParts(self.defDir)
path = ''
for part in parts:
path = os.path.join(path, part)
items = self.itemHandleMap.items()
for key,val in items:
if string.lower(val) == string.lower(path):
self.list.SelectItem(key)
if self.IsExpandable(val):
try:
self.list.Expand(key, commctrl.TVE_EXPAND)
except:
pass
def GetText(self, item):
ret = os.path.basename(item)
if not ret:
ret = item
return ret
def GetSubList(self, item):
if os.path.isdir(item):
try:
ret = map(lambda path, base=item:
os.path.join(base, path), os.listdir(item))
ret = filter(os.path.isdir, ret)
except os.error:
ret = None
elif item == 'My Computer':
possible = string.splitfields(win32api.GetLogicalDriveStrings(),
'\000')
ret = []
for drive in possible:
dType = win32file.GetDriveType(drive)
if (drive and
dType == win32con.DRIVE_FIXED or
dType == win32con.DRIVE_REMOTE):
try:
os.listdir(drive)
ret.append(string.upper(drive))
except os.error:
pass
else:
ret = None
if ret is None:
return ret
else:
ret.sort()
return ret
def IsExpandable(self, item):
return os.path.isdir(item)
def GetSelectedBitmapColumn(self, item):
return self.GetBitmapColumn(item)+6
def TakeDefaultAction(self, item):
return PerformItemSelected(item)
def PerformItemSelected(self, item):
self.choice = item
w = len(self.title)
self.dlg.SetWindowText(self.title+' ...%s' % item[-(49-w):])
def DirectoryDialog(title, topDir, defDir):
dirList = DirHierList('My Computer', defaultDir=defDir)
dirList.title = title
dlg = hierlist.HierDialog(title, dirList)
dirList.dlg = dlg
if dlg.DoModal() == win32con.IDOK:
return dirList.choice
else:
return None
if __name__== '__main__':
direc = DirectoryDialog('Test of directory selection',
'My Computer',
'C:\\CRI\\Images')
win32ui.MessageBox('You chose: %s' % direc)
-------------------------------------------------------------------
Cheers,
Roger Burnham
rburnham at cri-inc.com
More information about the Python-list
mailing list