6

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'
11

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 a panel exclusively for your plugin using QgsMessageLog class:

from qgis.core import Qgis
QgsMessageLog.logMessage( "Info message from plugin", "My Plugin", Qgis.Info )
QgsMessageLog.logMessage( "My warning message", "My Plugin", Qgis.Warning )
QgsMessageLog.logMessage( "My critical message!!!", "My Plugin", Qgis.Critical )
QgsMessageLog.logMessage( "My success message", "My Plugin", Qgis.Success )

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

from qgis.PyQt.QtWidgets import QDockWidget
consoleWidget = iface.mainWindow().findChild( QDockWidget, 'PythonConsole' )
consoleWidget.console.shellOut.clearConsole()
3
  • 1
    That works !! Thanks for the info regarding Log messages panel
    – Sjs
    Nov 3 '16 at 13:37
  • BTW, is there a command available to clear the previously logged messages upon new execution? @Germán Carrillo
    – Sjs
    Nov 9 '16 at 12:58
  • Rapidly skimming through the API, I couldn't find such command. Nov 9 '16 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.