从 GtkTreeView 获取完整文件路径

Get full file path from GtkTreeView

因此,我找到了使用 Gtk.TreeView 创建文件浏览器的教程,但我遇到了一个问题,当我 select 文件夹中的文件时,我无法获取文件的完整路径。我可以获得模型路径,但我不知道如何处理它。

这是我的项目树:

.
├── browser
│   ├── index.html
│   └── markdown.min.js
├── compiler.py
├── ide-preview.png
├── __init__.py
├── main.py
├── __pycache__
│   ├── compiler.cpython-35.pyc
│   └── welcomeWindow.cpython-35.pyc
├── pyide-settings.json
├── README.md
├── resources
│   └── icons
│       ├── git-branch.svg
│       ├── git-branch-uptodate.svg
│       └── git-branch-waitforcommit.svg
├── test.py
├── WelcomeWindow.glade
└── welcomeWindow.py

当我点击 main.py 时,路径是 4,但是如果我点击 browser/markdown.min.js,我得到 0:1

在我的代码中,我检查路径的长度(我用':'分割路径)是否大于1,如果不是我正常打开文件,如果是...这就是我卡住的地方.有人可以帮忙吗?

这是我的 TreeSelection 功能更改:

def onRowActivated(self, selection):
    # print(path.to_string()) # Might do the job...
    model, row = selection.get_selected()
    if row is not None:
        # print(model[row][0])
        path = model.get_path(row).to_string()
        pathArr = path.split(':')
        fileFullPath = ''

        if not os.path.isdir(os.path.realpath(model[row][0])):
            # self.openFile(os.path.realpath(model[row][0]))

            if len(pathArr) <= 1:
                self.openFile(os.path.realpath(model[row][0]))
            else:
                # Don't know what to do!

            self.languageLbl.set_text('Language: {}'.format(self.sbuff.get_language().get_name()))


    else:
        print('None')

完整代码可在 https://github.com/raggesilver/PyIDE/blob/master/main.py

获得

编辑 1:更具体地说,我的问题是当我从 TreeView 获取文件名时,我无法获取它之前的路径,所以我得到 index.html 而不是browser/index.html.

我找到了解决问题的方法,逻辑是向后遍历路径(例如:4:3:5:0)并获取最后一个 parent 的名称,然后添加到路径变量中.所以我们有:

def onRowActivated(self, selection):

    model, row = selection.get_selected()

    if row is not None:

        fullPath = ''
        cur = row

        while cur is not None:

            fullPath = os.path.join(model[cur][0], fullPath)
            cur = model.iter_parent(cur)

        # do whatever with fullPath