从GtkTreeView获取完整文件路径
问题描述:
因此,我找到了一个关于使用Gtk.TreeView创建文件浏览器的教程,但是当我选择文件夹内的文件时,我遇到了一个问题,我无法获取文件的完整路径。我可以得到模型路径,但我不知道如何处理它。从GtkTreeView获取完整文件路径
这是我的项目树:
.
├── 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)并获取最后一个父节点的名称,然后将其前置到路径变量。所以我们有:
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 len(pathArr) <= 1:
if not os.path.isdir(os.path.realpath(os.path.join(self.projectPath, model[row][0]))):
self.openFile(os.path.realpath(os.path.join(self.projectPath, model[row][0])))
self.autoComplete.on_document_load()
else:
exp = self.sideScroller.get_child().row_expanded(model.get_path(row))
if not exp:
self.sideScroller.get_child().expand_row(model.get_path(row), False)
else:
self.sideScroller.get_child().collapse_row(model.get_path(row))
else: ## HERE IS THE SOLUTION
p = model[row][0] # LAST ITEM IN PATH
i = model.iter_depth(row)
j = i
cur = None
while j > 0: # FOR EACH PARENT ADD THE LAST TO p
cur = model.iter_parent(cur) if not cur is None else model.iter_parent(row)
p = model[cur][0] + '/' + p # p = LAST PARENT + '/' + p
j -= 1
if not os.path.isdir(os.path.realpath(os.path.join(self.projectPath, p))):
self.openFile(os.path.realpath(os.path.join(self.projectPath, p)))
self.autoComplete.on_document_load()
else:
exp = self.sideScroller.get_child().row_expanded(model.get_path(row))
if not exp:
self.sideScroller.get_child().expand_row(model.get_path(row), False)
else:
self.sideScroller.get_child().collapse_row(model.get_path(row))
self.languageLbl.set_text('Language: {}'.format(self.sbuff.get_language().get_name() if not self.sbuff.get_language() is None else "Plain"))
else:
print('None')
当你使用'self.openFile(os.path.realpath(model [row] [0]))''会发生什么? – theGtknerd
我不认为它是相关的,但无论如何...我使用开放(路径,'r')来读取给定的文件,你可以检查Github上的所有功能,如果你想 –