Search code examples
python-3.xpyqtpyqt5qt-designer

Calculate with two QSliders values using PyQt5


i am learning to use PyQt5 and one function that can be useful to me is sliders/dials value that i can calculate with an other (for example, i am working on a python app to calculate coil inductance).

But all the tutorials i've found that far online, are simple codes to display on a label.

Generally it looks like this :

from PyQt5.QtWidgets import QMainWindow, QApplication, QSlider, QLabel
from PyQt5 import uic
import sys

class UI(QMainWindow):
    def __init__(self):
        super(UI,self).__init__()

        # Load the ui file
        uic.loadUi("Vsliders.ui", self)

        #define widgets
        self.slider         = self.findChild(QSlider,"slider")
        self.slider_value   = self.findChild(QLabel,"slider_value")

        # Slider properties
        self.slider.setMinimum(0)
        self.slider.setMaximum(100)
        self.slider.setValue(10)
        self.slider.setTickPosition(QSlider.TicksRight)
        self.slider.setTickInterval(5)
        self.slider.setSingleStep(5)

        # Move the slider
        self.slider.valueChanged.connect(self.slide_it)

        #show the App
        self.show()

    def slide_it(self, value):
        self.slider_value.setText(str(value))

#Initialize the app
app = QApplication(sys.argv)
UIWindow = UI()
app.exec_()

I understand we connect the only value on the slide_it function. But how do we hook up 2 values for example ?

like : how can i grab the 'int' value of the slider ? and use it to calculate ?

i tried this :

        self.slider.valueChanged.connect(self.slide_it)
        self.slider_2.valueChanged.connect(self.slide_it)

        #show the App
        self.show()

    def slide_it(self, value, value2):
        self.slider_value.setText(str(value + value2))

I was expecting it to take slide 1 on value and slider 2 on value2.

But it returns :

TypeError: UI.slide_it() missing 1 required positional argument: 'value2'

insted of

self.slider.valueChanged.connect(self.slide_it)

i tried

self.slider.value.connect(self.slide_it)

# or 

test = self.slider.value() # as a variable value 

    # for example adding directly both values in my function

    def slide_it(self):
        self.slider_value.setText(str(self.slider.value() + self.slider_2.value() ))

but it turns out those functions cannot be directly used as variable value.

TypeError: native Qt signal is not callable

Also .value() doesn't make anything moving. I think it really takes .valueChanged attribute.

i also tried this which is not exactly what i want but it does a result

    def slide_it(self, value):
        self.slider_value.setText(str(value + 2))

This way i get the to added to the initial value, displayed on a label. and yes it works. but this a value i add manually.

i guess the only method i know in that case, are used for one slider value at once.


Solution

  • ok thanks peoples for help !

    for the raised TypeError, probably it was a synthax matter.

    it worked that way :

        # Move the slider
        self.slider.valueChanged.connect(self.slide_it)
        self.slider_2.valueChanged.connect(self.slide_it)
    
        #show the App
        self.show()
    
    def slide_it(self):
        self.slider_value.setText(str(self.slider.value() + self.slider_2.value()))