GenericDirCtrl

I want to show a specific folder as the root of GenericDirCtrl, as
opposed to showing my computer's drives as the root. Is this possible?

I don't think it supports that, but it shouldn't be too hard to make your own wx.TreeCtrl class that does.

···

On 11/30/10 1:28 AM, sunspider wrote:

I want to show a specific folder as the root of GenericDirCtrl, as
opposed to showing my computer's drives as the root. Is this possible?

--
Robin Dunn
Software Craftsman

Okay, I made a file tree control. I thought it might be useful for
others so here it is.
The only thing I didn't figure out is how to set the root to the
"Computer". That is, I'm not sure how you set it so that the lowest
levels are the actual drives on your computer. If anyone knows how to
name that place (on windows it's "My Computer"), that would be helpful
towards making this code more robust. Although what I have works for
my current purposes.

···

-------------------------------------
import os
from fnmatch import fnmatch

import wx

class FileTree(wx.TreeCtrl):
    """FileTree(self, parent, id=-1, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=wx.TR_DEFAULT_STYLE,
                 validator=wx.DefaultValidator, name="",
                 rootfolder=None, file_filter=("*.*"))

rootfolder: the starting folder. Default to "."
file_filter: a list of patterns to match filenames against. for
example:
    ("*.py","*.pyw","python.*")

Note: this control ignores files that start with '.'.
"""
    def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=wx.TR_DEFAULT_STYLE|
wx.TR_HIDE_ROOT,
                 validator=wx.DefaultValidator, name="",
                 rootfolder=".", file_filter=("*.*")):
        wx.TreeCtrl.__init__( self, parent, id, pos, size, style,
validator,
                              name)
        self.file_filter = file_filter
        il = wx.ImageList(16,16)
        self.fldridx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER,
                                                       wx.ART_OTHER,
(16,16)))
        self.fldropenidx =
il.Add(wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN,
                                                          wx.ART_OTHER,
(16,16)))
        self.fileidx =
il.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE,
                                                       wx.ART_OTHER,
(16,16)))

        self.AssignImageList(il)
        root = self.AddRoot(os.path.split(rootfolder)[1])
        self.SetItemImage(root,self.fldridx,wx.TreeItemIcon_Normal)
        self.SetItemImage(root,
self.fldropenidx,wx.TreeItemIcon_Expanded)

        self.AddTreeNodes(root, rootfolder)
        try:
            self.Expand(root)
        except:
            # not if we have a hidden root
            pass

    def AddTreeNodes(self, parentItem, rootfolder):
        items = os.listdir(rootfolder)
        items = sorted(items,key=str.lower)
        folders =
        files =
        for item in items:
            if item[0]==".":
                continue
            itempath = os.path.join(rootfolder, item)
            if os.path.isfile(itempath):
                fileok=False
                for filter in self.file_filter:
                    if fnmatch(item, filter):
                        fileok=True
                        break
                if fileok:
                    files.append((item,itempath))
            else:
                folders.append((item,itempath))
        for folder, itempath in folders+files:
            newItem = self.AppendItem(parentItem, folder)
            if os.path.isfile(itempath):
                self.SetItemImage(newItem,
self.fileidx,wx.TreeItemIcon_Normal)
            else:
                self.SetItemImage(newItem,
self.fldridx,wx.TreeItemIcon_Normal)
                self.SetItemImage(newItem, self.fldropenidx,
                                  wx.TreeItemIcon_Expanded)
                self.AddTreeNodes(newItem, itempath)
            self.SetPyData( newItem, itempath)

    def GetFilePath(self):
        return self.GetPyData(self.GetSelection())

On Nov 30, 9:21 am, Robin Dunn <ro...@alldunn.com> wrote:

On 11/30/10 1:28 AM,sunspiderwrote:

> I want to show a specific folder as the root of GenericDirCtrl, as
> opposed to showing my computer's drives as the root. Is this possible?

I don't think it supports that, but it shouldn't be too hard to make
your own wx.TreeCtrl class that does.

--
Robin Dunn
Software Craftsmanhttp://wxPython.org

I’d love to see a wxPython.org controlled user-group or forum where recipes like this could find a better home. While staying with Google-Groups would be consistant with python-issued forums I personally find the google-groups harder to search for old stuff then ActiveState but less annoying then Daniweb.

Why do I feel wxpython-users@google.com isn’t the best place? Simply pasting code here in these forums is well, yuck. No highlighting, togglable line-numbering, and stuff like that. Funny that Google claims to heavily use Python internally.

    I'd love to see a wxPython.org controlled user-group or forum

where recipes like this could find a better home. While staying
with Google-Groups would be consistant with python-issued forums
I personally find the google-groups harder to search for old
stuff then ActiveState but less annoying then Daniweb.

Why do I feel wxpython-users@google.com
isn’t the best place? Simply pasting code here in these forums
is well, yuck.

Its a bit better if one attaches the code - not always convenient

so.

    No highlighting, togglable line-numbering, and stuff like

that. Funny that Google claims to heavily use Python internally.

I think the wiki is a very good place for that.

And within it probably here for something like this:

Werner

···

http://wiki.wxpython.org/

http://wiki.wxpython.org/RecipesControls

I've never used a forum I liked. I far prefer mailing lists, though Google groups is not ideal.

But anyway, recipes like this one would ideally be put in the Wiki -- that's the best place for such things -- that way comments and improvements can be done in place.

-Chris

···

On 12/2/10 5:56 AM, Dev Player wrote:

I'd love to see a wxPython.org controlled user-group or forum where
recipes like this could find a better home.

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

I don’t know. To me the Wiki is semi controlled and is more a semi formal thing, less formal then the docs but not filled with abbreviated chatter and the like these “user groups” or “forums”. I find more of wxpython recipes on ActiveState then the wiki, and there is more of a open discussion on various styles for a recipe with minor differences between them then on the wiki. Those forums seem much easier to follow the thread of an evolving recipe where the Wiki it’s more like “here, this is a recipe that does this, adapt it if you want.”. I haven’t seen pretty much any discussion on the wiki on a recipes. The wiki seems more a tutorial style (which I like as it is).

imho

Apologies to the original poster for getting off topic.

There is wx.GenericDirCtrl, which already does this in a cross-platform way.

- Josiah

···

On Wed, Dec 1, 2010 at 5:25 PM, sunspider <sunspider@gmail.com> wrote:

Okay, I made a file tree control. I thought it might be useful for
others so here it is.
The only thing I didn't figure out is how to set the root to the
"Computer". That is, I'm not sure how you set it so that the lowest
levels are the actual drives on your computer. If anyone knows how to
name that place (on windows it's "My Computer"), that would be helpful
towards making this code more robust. Although what I have works for
my current purposes.

-------------------------------------
import os
from fnmatch import fnmatch

import wx

class FileTree(wx.TreeCtrl):
"""FileTree(self, parent, id=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.TR_DEFAULT_STYLE,
validator=wx.DefaultValidator, name="",
rootfolder=None, file_filter=("*.*"))

rootfolder: the starting folder. Default to "."
file_filter: a list of patterns to match filenames against. for
example:
("*.py","*.pyw","python.*")

Note: this control ignores files that start with '.'.
"""
def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.TR_DEFAULT_STYLE|
wx.TR_HIDE_ROOT,
validator=wx.DefaultValidator, name="",
rootfolder=".", file_filter=("*.*")):
wx.TreeCtrl.__init__( self, parent, id, pos, size, style,
validator,
name)
self.file_filter = file_filter
il = wx.ImageList(16,16)
self.fldridx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER,
wx.ART_OTHER,
(16,16)))
self.fldropenidx =
il.Add(wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN,
wx.ART_OTHER,
(16,16)))
self.fileidx =
il.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE,
wx.ART_OTHER,
(16,16)))

   self\.AssignImageList\(il\)
   root = self\.AddRoot\(os\.path\.split\(rootfolder\)\[1\]\)
   self\.SetItemImage\(root,self\.fldridx,wx\.TreeItemIcon\_Normal\)
   self\.SetItemImage\(root,

self.fldropenidx,wx.TreeItemIcon_Expanded)

   self\.AddTreeNodes\(root, rootfolder\)
   try:
       self\.Expand\(root\)
   except:
       \# not if we have a hidden root
       pass

def AddTreeNodes(self, parentItem, rootfolder):
items = os.listdir(rootfolder)
items = sorted(items,key=str.lower)
folders =
files =
for item in items:
if item[0]==".":
continue
itempath = os.path.join(rootfolder, item)
if os.path.isfile(itempath):
fileok=False
for filter in self.file_filter:
if fnmatch(item, filter):
fileok=True
break
if fileok:
files.append((item,itempath))
else:
folders.append((item,itempath))
for folder, itempath in folders+files:
newItem = self.AppendItem(parentItem, folder)
if os.path.isfile(itempath):
self.SetItemImage(newItem,
self.fileidx,wx.TreeItemIcon_Normal)
else:
self.SetItemImage(newItem,
self.fldridx,wx.TreeItemIcon_Normal)
self.SetItemImage(newItem, self.fldropenidx,
wx.TreeItemIcon_Expanded)
self.AddTreeNodes(newItem, itempath)
self.SetPyData( newItem, itempath)

def GetFilePath(self):
return self.GetPyData(self.GetSelection())

On Nov 30, 9:21 am, Robin Dunn <ro...@alldunn.com> wrote:

On 11/30/10 1:28 AM,sunspiderwrote:

> I want to show a specific folder as the root of GenericDirCtrl, as
> opposed to showing my computer's drives as the root. Is this possible?

I don't think it supports that, but it shouldn't be too hard to make
your own wx.TreeCtrl class that does.

--
Robin Dunn
Software Craftsmanhttp://wxPython.org

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en