1

I am starting to learn to develop stand alone QGIS applications from a book called 'Building Mapping Applications with QGIS'.

Here's the python code stated in the book (lex.py):

import os, os.path, sys

from qgis.core import *
from qgis.gui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MapExplorer(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowTitle("Landmark Explorer")
        self.resize(800, 400)

def main():
    QgsApplication.setPrefixPath(os.environ['QGIS_PREFIX'], True)
    QgsApplication.initQgis()

    app = QApplication(sys.argv)

    window = MapExplorer()
    window.show()
    window.raise_()
    app.exec_()
    app.deleteLater()
    QgsApplication.exitQgis()

if __name__ == "__main__":
    main()

Batch file (run.bat):

SET OSGEO4W_ROOT=C:\OSGeo4W64
SET QGIS_PREFIX=%OSGEO4W_ROOT%\apps\qgis
SET PATH=%QGIS_PREFIX%\bin;%OSGEO4W_ROOT%\bin;%PATH%
SET PYTHONPATH=%QGIS_PREFIX%\python;%PYTHONPATH%
SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python27
python lex.py

However, when i run the batch file. it just displays this: CMD Window

William Ernesto
  • 163
  • 1
  • 7

1 Answers1

1

When running your script I receive a message that python.exe had crashed, I was able to isolate it to this particular line of code:

QgsApplication.initQgis()

Following the advice in this question on a similar error, just change the above line to this:

qgs = QgsApplication(sys.argv, False)
qgs.initQgis()

Python will still crash on my computer when the program is exited, but the expected blank window will appear.

chrki
  • 2,655
  • 16
  • 22