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
Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Jun 16, 2022 at 21:12
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
answered Jun 17, 2022 at 0:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.