Skip to content

Instantly share code, notes, and snippets.

@cpascual
Last active May 12, 2020 09:46
Show Gist options
  • Save cpascual/d8122147d6f672d9821917876c867745 to your computer and use it in GitHub Desktop.
Save cpascual/d8122147d6f672d9821917876c867745 to your computer and use it in GitHub Desktop.
a taurus-ified QLabel accepting 2 models
import sys
import taurus
from taurus.external.qt import Qt
from taurus.qt.qtgui.application import TaurusApplication
from taurus.qt.qtgui.base import TaurusBaseComponent
class MyLabel(Qt.QLabel, TaurusBaseComponent):
"""A taurus-ified QLabel that sums 2 attributes"""
def __init__(self, parent=None):
# call the parent class init
Qt.QLabel.__init__(self, "waiting...", parent=parent)
TaurusBaseComponent.__init__(self, "MyLabel")
self.setMinimumWidth(100)
def setModel(self, models):
m1,m2 = models
self._m1=taurus.Attribute(m1)
self._m2=taurus.Attribute(m2)
self._m1.addListener(self)
self._m2.addListener(self)
def handleEvent(self, evt_src, evt_type, evt_val):
"""reimplemented from TaurusBaseComponent
"""
# TaurusBaseComponent.handleEvent does nothing, so no need to call it
self.setText("{}".format(self._m1.read().rvalue + self._m2.read().rvalue))
if __name__ == "__main__":
# Initialize a Qt application (Qt will crash if you do not do this first)
app = TaurusApplication()
# instantiate the widget
w = MyLabel()
model1 = "eval:rand()"
model2 = "eval:10+rand()"
w.setModel((model1, model2))
# show it (if you do not show the widget, it won't be visible)
w.show()
# Initialize the Qt event loop (and exit when we close the app)
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment