4

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
0

1 Answer 1

5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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