问题
I want to show my folder as top directory in wxPython's GenericDirCtrl component. I tried SetPath() and path in my code but it only focuses the selected folder , not making it the top of the tree.
In my form's constructor i create it like that:
self.folder_tree_project = wx.GenericDirCtrl(self.pnl_edit, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(200, -1), wx.DIRCTRL_3D_INTERNAL | wx.SUNKEN_BORDER, wx.EmptyString, 0)
self.folder_tree_project.ShowHidden(False)
bSizer5.Add(self.folder_tree_project, 0, wx.ALL | wx.EXPAND, 5)
and then i am trying to change the top directory by using the working directory in my config file:
if self.config.workdir != "":
# self.folder_tree_project.SetPath(self.config.workdir)
# self.folder_tree_project.Path=self.config.workdir
both only focuses. Not changing the top directory.
Is there any way or attribute to handle it?
回答1:
You can use the wx.GenericDirList GetTreeCtrl() function to get the pointer to its wx.TreeCtrl attribute, and then use the TreeCtrl.AppendItem() to append the name of the directory you wanted to its root item.
its only the name of the directory however, and You're going to have to code its functionality, by binding mouse clicks and appending the directory's child files and child directories using os.walk (or by any other means), while working with the wx.TreeCtrl pointer.
maybe there's a better, embeded in wxpython api, way to do this, but i could'nt find anything in the documentation.
anyways here's the code for Adding the directory name to the wx.TreeCtrl pointer:
DirectoryNameHere = "mypath"
Bsizer = wx.BoxSizer(wx.VERTICAL)
self.folder_tree_project = wx.GenericDirCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(200, -1), wx.DIRCTRL_3D_INTERNAL | wx.SUNKEN_BORDER, wx.EmptyString, 0)
self.folder_tree_project.ShowHidden(False)
Tree = self.folder_tree_project.GetTreeCtrl()
Tree.AppendItem(Tree.GetRootItem(), DirectoryNameHere)
Bsizer.Add(self.folder_tree_project,1,wx.ALL | wx.EXPAND)
self.SetSizer(Bsizer)
来源:https://stackoverflow.com/questions/36865659/how-to-set-the-genericdirctrl-to-show-custom-folder-as-top-directory-in-wxpython