2017-04-07 164 views
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

+0

當你使用'self.openFile(os.path.realpath(model [row] [0]))''會發生什麼? – theGtknerd

+0

我不認爲它是相關的,但無論如何...我使用開放(路徑,'r')來讀取給定的文件,你可以檢查Github上的所有功能,如果你想 –

回答

0

我找到了我的問題的解決方案,邏輯是反向遍歷路徑(例如: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')