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.

clearhistory()[source]

Clear history buffer

event(ev)[source]

Event handler: handles special keys.

tabindent(forward=True)[source]

Add/remove blanks to next/previous multiple of 4 position

getcompletions(text)[source]

Return possible completions of text in context

returnkey()[source]

Handle return key. Record line and emit newline signal

recall(index)[source]

Select a line from the history list

record(line)[source]

Add line to history buffer

printhistory()[source]

Print the history to stdout

savehistory(filename, maxlines=0)[source]

Save the history to file

loadhistory(filename)[source]

Load a history file

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
setcontext(context)[source]

Set context for interpreter

resetbuffer()[source]

Reset the input buffer.

savehistory(filename)[source]

Save history to a file

saveoutput(filename, contents='all')[source]

Save the console output to a file

Parameters:
  • filename (str) – File name for the saved output. Existing file will be overwritten.

  • contents (str) – One of ‘all’, ‘commands’ or ‘output’. The first character suffices. Determines what to write: all output, only commands, or only the output resulting from the commands.

See also

savehistory

save the command history to a file.

push(line: str)[source]

Execute entered command. Command may span multiple lines

setfont(font: QFont)[source]

Set font for input and display widgets. Should be monospaced

write(line, color=None)[source]

Capture stdout and display in outdisplay

writeerror(line: str)[source]

Capture stderr and display in outdisplay

writecolor(text, color=None)[source]

Color can be anything accepted by QColor

movetoend()[source]

Move the output cursor to the end.

keyPressEvent(self, a0: QKeyEvent)[source]

55.2. Functions defined in module gui.pyconsole

gui.pyconsole.help(obj=None)[source]

Print help anbout any object