PyQt5 QSpinBox – Connecting two spin boxes with each other

In this article we will see how we can connect two spin boxes with each other such that every time value change in the one spin box should reflect in other spin box as well for example when we have fixed the width to height ratio of image and allow user to set width and height using spin box, as ratio is fixed therefore change in any dimension should also reflect in the other dimension. Example : Two spin box connected to each other such that their value should remain equal for every change of value occur in any of the spin box.
Steps for implementation : 1. Create two spin box 2. Add geometry to both the spin box 3. Add action to each spin box using valueChanged signal 4. Inside the first spin box action get the current value of spin box and set this value to the second spin box 5. Inside the second spin box action get the current value of spin box and set that value to the first spin box
Below is the implementation
Python3
# importing librariesfrom PyQt5.QtWidgets import *from PyQt5 import QtCore, QtGuifrom PyQt5.QtGui import *from PyQt5.QtCore import *import sysclass Window(QMainWindow):    def __init__(self):        super().__init__()        # setting title        self.setWindowTitle("Python ")        # setting geometry        self.setGeometry(100, 100, 600, 400)        # calling method        self.UiComponents()        # showing all the widgets        self.show()        # method for widgets    def UiComponents(self):        # creating spin box        self.spin1 = QSpinBox(self)        # setting geometry to spin box        self.spin1.setGeometry(100, 100, 150, 40)        # setting prefix to spin        self.spin1.setPrefix("Width : ")        # add action to this spin box        self.spin1.valueChanged.connect(self.action_spin1)        # creating another spin box        self.spin2 = QSpinBox(self)        # setting geometry to spin box        self.spin2.setGeometry(300, 100, 150, 40)        # setting prefix to spin box        self.spin2.setPrefix("Height : ")        # add action to this spin box        self.spin2.valueChanged.connect(self.action_spin2)    # method called after editing finished    def action_spin1(self):        # getting current value of spin box        current = self.spin1.value()                 # setting this value to second spin box        self.spin2.setValue(current)        # method called after editing finished    def action_spin2(self):        # getting current value of spin box        current = self.spin2.value()                 # setting this value to the first spin box        self.spin1.setValue(current)# create pyqt5 appApp = QApplication(sys.argv)# create the instance of our Windowwindow = Window()# start the appsys.exit(App.exec()) | 
Output :
				
					


