pylab_ml.gui.instruments.base_instrument.Gui

class Gui(grandparent=None, name=None, parentwindow=None)[source]

Bases: object

Base Class for Gui Applikations with an Sensor/Instrument Needs:

  • mqtt-message from the parent

Provides:
  • basic-Window for Instruments

  • receive and show mqtt_status in the statusbar

  • subtopic

  • function mqttConnectWidgets: if Objects-name or an svg-id start with MQTT than automatically connect this object to gui2mqtt() and send the new value about mqtt recognise QT-objects: QCombobox, QPushButton, QDoubleSpinBox, QSpinBox, QLCDNumber, QLabel

  • if the Gui starts, it send the mqtt connect message if the Gui receive a mqtt connect, it send the the mqtt get from the list self.mqtt_initlist + self.mqtt_cmds

__init__(grandparent=None, name=None, parentwindow=None)[source]

Init the Gui, create a window and load the ui-file with the same name as the class.

Parameters:
  • grandparent (object, optional) – The parent object that contains the MQTT client and other shared resources. The default is None.

  • name (string, optional) – The name of the instrument, used for MQTT topics and window title. The default is None.

  • parentwindow (QtWidgets.QWidget, optional) – The parent window for the GUI. The default is None.

Methods

__init__([grandparent, name, parentwindow])

Init the Gui, create a window and load the ui-file with the same name as the class.

add_menue(menue, connect, enabled[, ...])

Add a menue item to Setup

add_menuicon(typ[, connect])

Add icon and action to the menu.

adjustUI()

Adjust the UI, set the title and icons and connect the quit action.

close([event])

Close the GUI, publish a disconnect message and call the appclosed method of the grandparent if it exists.

dprint(msg)

Print a debug message if debugging is enabled.

filtersubtopic(value)

Filter the subtopic from the value, if the value starts with the topinstname.

gui2mqtt(widget)

Function connect a Gui-widget to mqtt, it sends a mqtt set command with the value from the Gui-widget.

gui_disconnect()

Disconnect the GUI, set the MQTT status to "disconnect" and disable the frame.

hide()

Hide the GUI.

isVisible()

Return whether the GUI is currently visible.

mqttConnectSVGWidget(filename, name[, channel])

Create an InteractiveSvgWidget and load the filname.

mqttConnectWidgets()

Connect all object which start with MQTT_ to the function gui2mqtt().

mqttreceive(instName, msg[, check])

Common MQTT receive messages, get raw MQTT data for receiving more information.

objectName2mqttName(name)

Filter from the object name the right MQTT name.

publish(cmd[, value, instName])

Publish a command and value over MQTT.

retranslateUi(Form)

Retranslate the UI, set the text of the widgets.

setLabel(Label, value[, ext])

Set the text of a label.

set_Geometry(geometry)

Set the geometry of the GUI, check if the geometry is on the actual screen.

show()

Show the GUI.

svg2mqtt(name, value)

Publish the name and value over mqtt.

unitOfMeasurement(value[, unit])

Convert a value with a unit to its numerical representation.

Attributes

id

Return the id of the instrument, which is used for MQTT topics.

mqtt_status

Return the MQTT status of the instrument, which is either "connect" or "disconnect".

status

Return the current status message from the status bar.

subtopic

Return the subtopic of the instrument, which is the part after the topinstname.

add_menue(menue, connect, enabled, checkable=False, checked=False)[source]

Add a menue item to Setup

Parameters:
  • menue (string) – The text of the menu item.

  • connect (function) – The function to be called when the menu item is triggered.

  • enabled (bool) – Whether the menu item is enabled.

  • checkable (bool, optional) – Whether the menu item is checkable. The default is False.

  • checked (bool, optional) – Whether the menu item is checked. The default is False.

add_menuicon(typ, connect=None)[source]

Add icon and action to the menu.

Parameters:
  • typ (string) – The type of the menu item.

  • connect (function, optional) – The function to be called when the menu item is triggered. The default is None.W

“onoff” : on/off

adjustUI()[source]

Adjust the UI, set the title and icons and connect the quit action.

close(event=None)[source]

Close the GUI, publish a disconnect message and call the appclosed method of the grandparent if it exists.

dprint(msg)[source]

Print a debug message if debugging is enabled.

filtersubtopic(value)[source]

Filter the subtopic from the value, if the value starts with the topinstname.

gui2mqtt(widget)[source]

Function connect a Gui-widget to mqtt, it sends a mqtt set command with the value from the Gui-widget.

Parameters:

widget (QtWidgets.QWidget) – The GUI widget that triggered the MQTT command, expected to have an objectName that follows the

Return type:

None.

gui_disconnect()[source]

Disconnect the GUI, set the MQTT status to “disconnect” and disable the frame.

hide()[source]

Hide the GUI.

property id

Return the id of the instrument, which is used for MQTT topics.

isVisible()[source]

Return whether the GUI is currently visible.

mqttConnectSVGWidget(filename, name, channel=None)[source]

Create an InteractiveSvgWidget and load the filname. Connect all clicks to the text id’s to the function svg2mqtt().

Parameters:
  • filename (string) – The path to the SVG file to be loaded into the widget.

  • name (string) – The base name for the widget, used for MQTT topic construction.

  • channel (string, optional) – An optional channel identifier to be appended to the name for MQTT topic construction. The default is None.

Returns:

svgWidget – The created InteractiveSvgWidget with the loaded SVG file and connected signals.

Return type:

InteractiveSvgWidget

mqttConnectWidgets()[source]

Connect all object which start with MQTT_ to the function gui2mqtt().

Create a list with all commands in self.mqtt_cmds

property mqtt_status

Return the MQTT status of the instrument, which is either “connect” or “disconnect”.

mqttreceive(instName, msg, check=False)[source]

Common MQTT receive messages, get raw MQTT data for receiving more information.

Parameters:
  • instName (string) – The name of the instrument sending the MQTT message.

  • msg (dict) – The MQTT message received, expected to have keys “cmd”, “payload”, and “type”.

  • check (bool, optional) – If True, only process the message if the command is in self.mqtt_cmds. The default is False.

Returns:

result – True if the message was processed successfully, False otherwise.

Return type:

bool

objectName2mqttName(name)[source]

Filter from the object name the right MQTT name.

eg. objectname = MQTT_voltage -> Voltage listen on get and set

= MQTT_set_I_limit -> I_limit listen only on set = MQTT_get_I_limit -> I_limit listen only on get = TAB_ch -> Name for a tab-name

Parameters:

name (string) – The object name to be filtered.

Returns:

  • listen (string) – The type of MQTT command to listen for (“set”, “get”, or “”).

  • result (string) – The filtered MQTT name extracted from the object name.

publish(cmd, value='', instName=None)[source]

Publish a command and value over MQTT.

Parameters:
  • cmd (string) – The command to be published.

  • value (string, optional) – The value associated with the command. The default is an empty string.

  • instName (string, optional) – The name of the instrument. If None, the instance’s name is used. The default is None.

retranslateUi(Form)[source]

Retranslate the UI, set the text of the widgets.

setLabel(Label, value, ext=None)[source]

Set the text of a label.

Parameters:
  • Label (object) – the label object.

  • value (string) – this text will be displayed.

  • ext (string, optional) – extention, The default is None.

Return type:

None.

set_Geometry(geometry)[source]

Set the geometry of the GUI, check if the geometry is on the actual screen.

Parameters:

geometry (tuple) – A tuple of the form (x, y, width, height) representing the geometry of the GUI.

show()[source]

Show the GUI.

property status

Return the current status message from the status bar.

property subtopic

Return the subtopic of the instrument, which is the part after the topinstname.

svg2mqtt(name, value)[source]

Publish the name and value over mqtt. Overwrite this function if you need a more complex form.

Parameters:
  • name (string) – The name of the SVG element that was changed.

  • value (string) – The new value of the SVG element.

unitOfMeasurement(value, unit=None)[source]

Convert a value with a unit to its numerical representation.

Parameters:
  • value (string) – The value with a unit (e.g., “10 k”).

  • unit (string, optional) – The unit to use for conversion. If None, the unit is inferred from the value.

Returns:

The numerical representation of the value.

Return type:

float