1
因此,我找到了一個關於使用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
。
當你使用'self.openFile(os.path.realpath(model [row] [0]))''會發生什麼? – theGtknerd
我不認爲它是相關的,但無論如何...我使用開放(路徑,'r')來讀取給定的文件,你可以檢查Github上的所有功能,如果你想 –