2017-04-23 37 views
1

我使用PyQGIS Cookbook學習PyQGIS,併購買了PyQGIS程序員指南。我已經能夠使用小腳本加載圖層,但沒有運氣將項目加載到QGIS中。我不斷收到第13行的'SyntaxError:invalid syntax'。我正在使用QGIS中的Python控制檯並鍵入'import _loadproject'來啓動腳本。如何使用PyQGIS將項目加載到QGIS?

有人可以指出我要去哪裏嗎?

"""Load Project: This is to test loading a project.""" 

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
from qgis.core import * 
from qgis.utils import iface 

class ProjectScript: 

def __init__(self, iface): 
    self.iface = iface 

def load_project(self) 
    project = QgsProject.instance() \ 
    project.read(QFileInfo('S:/SOCAL/Section_28/SOCAL_CK47/Control_Data/SOCAL_CK47_project.qgs')) 
    print project.fileName 'S:/SOCAL/Section_28/SOCAL_CK47/Control_Data/SOCAL_CK47_project.qgs' 

def run_script(iface): 
"""Run the script by loading and calling LoadProject methods.""" 
fs = ProjectScript(iface) 
fs.load_project() 
+0

in line 13你在load_project函數中沒有「:」def load_project(self):「 –

回答

1

檢查這個例子:

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
from qgis.core import * 
from qgis.utils import iface 

class ProjectScript: 

    def __init__(self, iface): 
     self.iface = iface 

    def load_project(self): 
     project = QgsProject.instance() 
     project.read(QFileInfo('S:\\SOCAL\\Section_28\\SOCAL_CK47\\Control_Data\\SOCAL_CK47_project.qgs')) 
     print project.fileName() 

    def run_script(iface): 
     """Run the script by loading and calling LoadProject methods.""" 
     fs = ProjectScript(iface) 
     fs.load_project() 

保存這個腳本,例如ProjectScript.py使用Python控制檯,execute.The下一個圖像顯示效果。 enter image description here

+0

謝謝! - 腳本在Script Runner插件中工作,但不使用QGIS控制檯很有趣。我會深入挖掘。但是還有一個問題 - 當我手動加載一個項目時,我總是在QGIS的左上角獲得項目名稱(例如:QGIS 2.18.2 - SOCAL_CK47_project),但是當我運行腳本時不會。有沒有可用於刷新QGIS以顯示項目名稱的聲明? – Clutch

+0

試試這個:qgis.utils.iface.mainWindow()。setWindowTitle(「My Title」) –

相關問題