I am writing a small GUI with PyQt5 that links two series of values (let's call them a, b, c and alpha, beta respectively). All numbers are input via QDoubleSpinBox widgets.
These numbers can be computed from each other via two functions:
alpha, beta = f1(a,b,c)
a,b,c = f2(alpha, beta)
The problem is that since the signal of a,b,c triggers a slot modifying alpha,beta and vice-versa, I get stuck in an infinite loop of inter-updating widgets as soon as I change the value of something, until the UI crashes. When commenting out the the update along one or the other direction, things work fine.
Without the details of the GUI layout, this is basically what I am doing:
from PyQt5.QtWidgets import (QApplication, QWidget, QDoubleSpinBox)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
# initialize UI and widgets (not shown)
self.initUI()
# update alpha beta
self.a.valueChanged.connect(self.update_alphabeta)
self.b.valueChanged.connect(self.update_alphabeta)
self.c.valueChanged.connect(self.update_alphabeta)
# update abc
self.alpha.valueChanged.connect(self.update_abc)
self.beta.valueChanged.connect(self.update_abc)
def update_alphabeta(self):
alpha,beta = f1(self.a, self.b, self.c)
self.alpha.setValue(alpha)
self.beta.setValue(beta)
def update_abc(self):
a,b,c = f2(self.alpha, self.beta)
self.a.setValue(a)
self.b.setValue(b)
self.c.setValue(c)
I could not find a proper solution so far. Perhaps there is something fundamentally flawed in the way I am trying to do that.