I have 3 identical physical instruments. I can control each instrument separately by instanciating an InstrumentDriver
class.
I have a Ui_MainWindow
class, automatically created from Qt Designer (via pyuic5
). This class contains "duplicated" controls: I have 3 identical sets of UI controls (buttons, lineEdits, etc.), each one is used to control one instrument.
I have a InstrumentGui
class doing the mapping between the UI elements defined in Ui_MainWindow
and the actions allowed by InstrumentDriver
. InstrumentGui
is also instanciated 3 times.
I don't know how to bind a GUI control to a specific instance of InstrumentDriver
class:
class InstrumentGui :
def __init__(self, instrument: InstrumentDriver, ui: Ui_MainWindow, type: str)
# How do I reach toolButton_startA if type=="A",
# toolButton_startB if type == "B", etc.?
ui.toolButton_startA.clicked.connect(instrument.start)
ui = Ui_MainWindow()
instrumentA = InstrumentDriver(address=1)
instrumentGuiA = InstrumentGui(instrumentA, ui, "A")
instrumentB = InstrumentDriver(address=2)
instrumentGuiB = InstrumentGui(instrumentB, ui, "B")
instrumentC = InstrumentDriver(address=3)
instrumentGuiC = InstrumentGui(instrumentC, ui, "C")
My first idea was to use variable variable name, something like setattr(self, "start_button", "toolButton_start"+type)
, but appart from the fact that I didn't manage to get it work, I read everywhere that it is a bad practice.
What is the smart way of doing that, if possible keeping the Qt Designer way of building GUI layout?