Wednesday 28 January 2015

How to show Python console at QGIS program start


I wonder if there is a way to open the Python console of QGIS 2.6 directly at program launch. There is a possibility to set a shortcut for the Python console, but I can't find such an option for the QGIS program launch.



Answer



You can start QGIS Python console when opening a project by writing a couple of lines in QGIS->Project->Project Properties:


def openProject():
import qgis
qgis.utils.iface.actionShowPythonDialog().trigger()

Make sure you enable macros on your project, this way: Settings->Options->General->Enable macros: Always


As you want the QGIS Python console to open when launching QGIS, you can create (if it doesn't exist already) a startup.py file in .qgis2/python/ and write:



import qgis     
qgis.utils.iface.actionShowPythonDialog().trigger()



EDIT (to address a follow-up question by @Miro)


As pointed out by Miro, if QGIS Python Console is open, qgis.utils.iface.actionShowPythonDialog().trigger() will close it, so, if we are writing a QGIS plugin, it might make sense to know if the Python Console is open (visible) or not.


You can know if the Python Console is not visible (and then open it) by running this code:


from PyQt4.QtGui import QDockWidget
pythonConsole = iface.mainWindow().findChild( QDockWidget, 'PythonConsole' )
if not pythonConsole.isVisible():

pythonConsole.setVisible( True )

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...