How to set the GenericDirCtrl to show custom folder as top directory in wxpython?

隐身守侯 提交于 2020-01-07 01:52:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!