55. gui.pyconsole
— Interactive Python console in a Qt widget.¶
This module provides the PyConsole class: an interactive Python interpreter embedded in a Qt widget. PyConsole was created for the pyFormex GUI, but can be used independently in any Python Qt application. It can even be run as a standalone application. PyConsole works with either PyQt5 or PySide2.
Features:
accepts multiline statements
accepts continuation lines (ending in backslash)
keeps a command history with recall and save
colored output
indentation adjustement on multiple of 4 columns
built-in help function
built-in completer functionality
can be executed as a standalone program (see below)
easy to add more functionality
Special Keys:
RETURN: move input to output and execute if statement complete
LEFT: go left
RIGHT: go right
CTRL-LEFT: go to beginning of line
CTRL-END: go to end of line
UP: go up in history
DOWN: go down in history
CTRL-UP: go to first line in history
CTRl-DOWN: go to last line in history
TAB: if cursor is at start or after a space: increase indent to next multiple of 4; else: start completion of the text before the cursor
BACKTAB (SHIFT-TAB): if cursor is after a space: reduce indent to previous multiple of 4
Completion:
If TAB is pressed when the cursor is after a non-space, the built-in completer is started. It first looks up the possible completions for the text before the cursor. If None is found, it does nothing. If just one is found, it is immediately filled in. Another TAB press will remove it again. If multiple completions were found, it prints the list of possible completions on the output. Hitting TAB again will fill in the first completion, and repeatedly pressing TAB cycles through the whole list of completions until the last possiblility is removed again. Pressing any other key but TAB during the completion process will accept the currently filled in completion. Note that any text following the cursor also keeps in place following the completion.
To run the console as a standalone program, use the following command:
python3 pyconsole.py
You can specify the window geometry in X11 style:
python pyconsole.py --qwindowgeometry 800x600-0+0
This command will create a window of 800 by 600 pixels in the upper right corner of the screen.
55.1. Classes defined in module gui.pyconsole¶
- class gui.pyconsole.LineEdit(historysize=0)[source]¶
Input line with a history buffer for recalling previous lines.
- Parameters:
historysize (int) – Maximum number of lines kept in the history. If <= 0, the history size is unlimited.
- class gui.pyconsole.PyConsole(parent=None, context={}, historysize=0, blockcount=0, font='DejaVu Sans Mono', fontsize=10)[source]¶
An interactive Python console in a Qt widget.
PyConsole is a Qt5 widget exposing an interactive Python console. It combines a read-only output area and a one line input area. When pressing ENTER, it copies the input line to the output area and executes the statement if it is complete.
- Parameters:
parent (QWidget) – If provided, the PyConsole will be made a child of that widget.
context (
InteractiveInterpreter
| dict) – The context where the source will be executed. If an interpreter, it is used as is. If a dict, a new interpreter will be created with the specified dict as its locals.historysize (int) – Maximum number of lines in the history buffer. If 0, the history buffer is unlimited.
blockcount (int) – Maximum number of lines in the output buffer. If 0, the output buffer is unlimited.
fontname (str) – Name of the font to be used. It better be a monospace font!
fontsize (int) – The font size
- setFocus(self)[source]¶
- setFocus(self, reason: FocusReason) None
- saveoutput(filename, contents='all')[source]¶
Save the console output to a file
- Parameters:
See also
savehistory
save the command history to a file.