Build your first Taurus GUI in Python
This tutorial introduces the basic Taurus GUI building blocks by progressively
creating a small interface for a Tango device. You will start by displaying a
single attribute and then move to editable values and panel.TaurusForm.
Set up the example device
The device server used for the examples can be obtained
here.
For the examples to work as provided, create and run a TaurusTest device with the following configuration:
Server (ServerName/Instance):TaurusTest/taurustest
Class:TaurusTest
Devices:sys/taurustest/1
You can configure it from Jive by going to Edit -> Create server and
entering the parameters above.
Create the application window
Start with a minimal Qt application and a horizontal layout:
1import sys
2from taurus.external.qt import Qt
3from taurus.qt.qtgui.application import TaurusApplication
4
5app = TaurusApplication(sys.argv, cmd_line_parser=None)
6panel = Qt.QWidget()
7layout = Qt.QHBoxLayout()
8panel.setLayout(layout)
The following examples only show the code that changes. Unless otherwise stated, add the snippets between the code above and this common footer:
panel.show()
sys.exit(app.exec_())
Display an attribute value
A display.TaurusLabel can display a Tango attribute value. Add the
following code before the common footer:
from taurus.qt.qtgui.display import TaurusLabel
w = TaurusLabel()
layout.addWidget(w)
w.model = "sys/taurustest/1/position#rvalue.magnitude"
If the #rvalue.magnitude fragment is omitted from the model, the units are
also displayed when they are defined by the attribute.
Add the attribute label
Use the #label fragment to display the Tango label next to the value:
1from taurus.qt.qtgui.display import TaurusLabel
2
3w1, w2 = TaurusLabel(), TaurusLabel()
4layout.addWidget(w1)
5layout.addWidget(w2)
6w1.model, w1.bgRole = "sys/taurustest/1/position#label", ""
7w2.model = "sys/taurustest/1/position#rvalue.magnitude"
Add separate units
Taurus model fragments let different widgets display different parts of the same attribute model. Extend the previous example with a third label:
1from taurus.qt.qtgui.display import TaurusLabel
2
3w1, w2, w3 = TaurusLabel(), TaurusLabel(), TaurusLabel()
4layout.addWidget(w1)
5layout.addWidget(w2)
6layout.addWidget(w3)
7
8w1.model, w1.bgRole = "sys/taurustest/1/position#label", ""
9w2.model = "sys/taurustest/1/position#rvalue.magnitude"
10w3.model, w3.bgRole = "sys/taurustest/1/position#rvalue.units", ""
The interface now displays the attribute label, its current value, and its units independently.
Make the value editable
To let the user change the attribute, add a Taurus input widget for the write value. For example:
1from taurus.qt.qtgui.display import TaurusLabel
2from taurus.qt.qtgui.input import TaurusValueLineEdit
3
4w1 = TaurusLabel()
5w2 = TaurusLabel()
6w3 = TaurusValueLineEdit()
7w4 = TaurusLabel()
8
9layout.addWidget(w1)
10layout.addWidget(w2)
11layout.addWidget(w3)
12layout.addWidget(w4)
13
14w1.model, w1.bgRole = "sys/taurustest/1/position#label", ""
15w2.model = "sys/taurustest/1/position#rvalue.magnitude"
16w3.model = "sys/taurustest/1/position#wvalue.magnitude"
17w4.model, w4.bgRole = "sys/taurustest/1/position#rvalue.units", ""
Editing the write value with TaurusValueLineEdit.
Other Taurus input widgets, such as input.TaurusValueSpinBox and
input.TaurusWheelEdit, can be used in the same way.
Use a TaurusForm for several attributes
Creating individual widgets becomes tedious when you need to display many
attributes. panel.TaurusForm provides a higher-level interface that
selects suitable widgets for the models it receives.
For this step, replace the generic panel created earlier with a
TaurusForm:
1from taurus.qt.qtgui.panel import TaurusForm
2
3panel = TaurusForm()
4props = ["state", "status", "position", "velocity", "acceleration"]
5model = ["sys/taurustest/1/%s" % p for p in props]
6panel.setModel(model)
The form aligns the rows, manages write operations, and chooses appropriate widgets according to each model.
Customize the form
You can override the automatically selected widgets for individual rows:
from taurus.qt.qtgui.display import TaurusLabel
panel[0].readWidgetClass = TaurusLabel
panel[2].writeWidgetClass = "TaurusWheelEdit"
You can also customize the appearance with Qt stylesheets:
panel.setStyleSheet("font-size: 40px;")
The recommended way to change the style is to use setStyleSheet on the
TaurusForm (or another top-level widget). If you need to style a specific
widget inside a TaurusValue, use Qt methods directly:
from taurus.external.qt import Qt
for row in panel:
row.readWidget().setFont(Qt.QFont("Sans Serif", 40))
For more detailed form customization options, see Use Taurus forms.
Next steps
You now have a small Taurus application that can display and edit live Tango attributes and can scale from individual widgets to a complete form.
Continue with Plot data with Taurus to add live plots and trends, or see Build a GUI with the TaurusGUI framework to build a larger application using the TaurusGUI framework.