9 Python Scripts

Each widget can have a Python script that is executed whenever one of its script variables changes. This allows, for example, to enable or disable widgets or to change their appearance depending on process values.

9.1 Script editor

Select a widget in edit mode and open the Python Script dock (or select Edit script... from the context menu of the widget).

Python script editor

The table at the top contains the script variables. Drag process variables from the data source tree into the table. The Name column defines the name under which the variable is available in the script; Period [s] defines the sample time.

Note

Within one connection to a process, a signal can be transmitted either event-based (period 0) or periodically (period > 0) (see Browsing a data source).

The script must define a function update(), which is called when a variable changes. Within the script, widget refers to the widget the script belongs to.

# Enable widget based on the value of the 'enable' variable
def update():
    widget.setEnabled(enable.getValue())

Right-click into the editor to insert templates.

Script templates

9.2 Combining scripts and stylesheets

A script can set dynamic properties, which are evaluated by the stylesheet:

def update():
    widget.setProperty('highlight', highlight.getValue())
QWidget[highlight=true] {
    background-color: yellow;
}
Highlighting via script and stylesheet

9.3 Error messages

Syntax errors are displayed with the line number. If no callable update() is found, this is indicated as well.

Script error

9.4 Python shell

The Python Shell dock provides an interactive Python interpreter, which shares the environment with the scripts. Outputs of print() in scripts appear here.

Python shell

See the Python API reference for all available objects and functions.