QGIS Python – Showing Editor by Default in QGIS Python Console

pyqgispythonqgisqgis-python-console

I use startup.py file in folder %AppData%\QGIS\QGIS3 to open the Python Console at QGIS start.

Is there a way to also show the editor?

I browsed a lot of pages in the API, but didn't find the answer.

The code I use in startup.py is as follows:

from qgis.PyQt.QtWidgets import QDockWidget
from qgis.utils import iface #change made to original code

pythonConsole = iface.mainWindow().findChild(QDockWidget, 'PythonConsole')

if not pythonConsole or not pythonConsole.isVisible():
    iface.actionShowPythonDialog().trigger()

Credits to original code: Showing Python Console when QGIS starts

Using @Mayo's answer, I adapted the startup.py script, which now reads like follows:

import console
from qgis.utils import iface
from qgis.PyQt.QtWidgets import QDockWidget

pythonConsole = iface.mainWindow().findChild(QDockWidget, 'PythonConsole')

if not pythonConsole or not pythonConsole.isVisible():
    iface.actionShowPythonDialog().trigger()
    pythonConsole = iface.mainWindow().findChild(QDockWidget, 'PythonConsole')
    python_console = pythonConsole.findChild(console.console.PythonConsoleWidget)

    for w in python_console.children():
        try:
            if w.text() == 'Show Editor':
                w.trigger()
        except AttributeError:
            pass

Best Answer

Show the console by calling the trigger() function of the Show Editor button:

import console
    
python_widget = iface.mainWindow().findChild(QDockWidget, 'PythonConsole')
python_console = python_widget.findChild(console.console.PythonConsoleWidget)
    
for w in python_console.children():
    try:
        if w.text() == 'Show Editor':
            w.trigger()
            break
    except AttributeError:
        pass
Related Question