Wednesday 20 December 2017

pyqgis - Clearing Python Console in QGIS using Python Command


I'm developing a python script in which I use several "print" statements to view the intermediate results in python console of QGIS. Each time after executing the program, I have to manually clear the python console using "Clear Console" option.



Is there a way out to clear the printed statements in python console using a command?


I tried using the following command from this answer


qgis.console.clearConsole()

But I am getting this error message


AttributeError: 'module' object has no attribute 'console'

Answer



QGIS offers you several classes for logging messages into the Log Messages Panel (i.e., no need to print debug messages from your plugin into the QGIS Python console). You could even easily create a tab in such panel exclusively for your plugin using QgsMessageLog class:


QgsMessageLog.logMessage( "Info message from plugin", "My Plugin", 0 )
QgsMessageLog.logMessage( "Warning message from plugin", "My Plugin", 1 )

QgsMessageLog.logMessage( "Critical message from plugin!!!", "My Plugin", 2 )
QgsMessageLog.logMessage( "Info message from plugin", "My Plugin", 0 )

Having said that, you can clear the QGIS Python console with these 3 lines:


from PyQt4.QtGui import QDockWidget
consoleWidget = iface.mainWindow().findChild( QDockWidget, 'PythonConsole' )
consoleWidget.console.shellOut.clearConsole()

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...