Python Forum
Use custom root in wx.GenericDirCtrl
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use custom root in wx.GenericDirCtrl
#1
Hi Team,

I am playing around with GenericDirCtrl and would like to know how I can set a custom directory as the root. For example, the directory tree on my PC looks something like this:

/ <-- standard root of the directory
/bin
/bin/whatever
/boot
/data
/data/projects
/data/projects/projA
/data/projects/projB
/dev
/etc
/etc/whatever
/home
/home/user1
...

When I create the GenericDirCtrl it will properly display the entire tree. What I want to achieve is that the control shows me this:

/projects <-- custom root
/projects/projA
/projects/projB

...as if the rest of the directory structure does not exist.

Would this be possible? I have spent quite a bit of time searching the internet, but cannot find what I am looking for. I came across this
https://stackoverflow.com/questions/3686...n-wxpython

and tried to recreate it, but I couldn't get it to work.

I appreciate your suggestions.

Thank you.

Marc.
Reply
#2
The initializer has a dir argument, which the docs say set the default folder for showing results. Does that not do what you're aiming for?
Reply
#3
(Feb-23-2018, 04:35 PM)nilamo Wrote: The initializer has a dir argument, which the docs say set the default folder for showing results. Does that not do what you're aiming for?
It seems that using the "dir" parameter causes the tree to set focus on the /data/projects directory. At the same time, the entire tree is still visible and the user can navigate to the rest of the tree. My goal is to essentially "lock" the user into the /data/projects directory and not bother him/her with the rest of the directory structure. As such, I would like the root of the tree to say "projects", which can then be expanded into "projA", "projB", etc and subdirectories (if any) underneath the project directories.

I hope this makes sense...

Marc.

Not sure if this matters, but I am using wxFormBuilder to build the GUI. This is the code it has generated to build the tree:

self.gendirctrlDocExplorer = wx.GenericDirCtrl( self, wx.ID_ANY, u"/data/projects", wx.DefaultPosition, wx.DefaultSize, wx.DIRCTRL_3D_INTERNAL|wx.SUNKEN_BORDER, wx.EmptyString, 0 )
self.gendirctrlDocExplorer.ShowHidden( False )
bSizer5.Add( self.gendirctrlDocExplorer, 1, wx.ALL|wx.EXPAND, 5 )
Reply
#4
Alternatively, could/should I use a different control to create what I want to achieve? Perhaps a more generic tree control in combination with a routine to go over the directory tree and populate the tree with directories and files? If so, how would I approach that?
Reply
#5
OK, I think I have worked it out. For future reference:

import wx, os

class MyTree(wx.TreeCtrl):
    def __init__(self, *args, **kwargs):
        super(MyTree, self).__init__(*args, **kwargs)
        self.__collapsing = True

        il = wx.ImageList(16,16)
        self.folderidx = il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER, 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 = '/data/projects'
        ids = {root : self.AddRoot(root, self.folderidx)}
        self.SetItemHasChildren(ids[root])

        for (dirpath, dirnames, filenames) in os.walk(root):
            for dirname in sorted(dirnames):
                fullpath = os.path.join(dirpath, dirname)
                ids[fullpath] = self.AppendItem(ids[dirpath], dirname, self.folderidx)
                
            for filename in sorted(filenames):
                self.AppendItem(ids[dirpath], filename, self.fileidx)

class MyTreeFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(MyTreeFrame, self).__init__(*args, **kwargs)
        self.__tree = MyTree(self)


if __name__ == "__main__":
    app = wx.App(False)
    frame = MyTreeFrame(None)
    frame.Show()
    app.MainLoop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Tkinter custom widget styling and creating custom theme karolp 6 4,747 May-06-2020, 06:11 PM
Last Post: karolp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020