analitics

Pages

Monday, September 2, 2024

Python Qt6 : Two sliders

I used the Python 3.13.0rc1 version and PyQt6 6.7.1 version.
This is the source code:
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QSlider
from PyQt6.QtCore import Qt

class SliderWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Two Slideres")
        self.setGeometry(100, 100, 640, 200)

        layout = QVBoxLayout()

        self.slider_max = QSlider(Qt.Orientation.Horizontal)
        self.slider_max.setMinimum(0)
        self.slider_max.setMaximum(100)
        self.slider_max.setValue(100)
        self.slider_max.valueChanged.connect(self.update_min_slider)

        self.slider_min = QSlider(Qt.Orientation.Horizontal)
        self.slider_min.setMinimum(0)
        self.slider_min.setMaximum(100)
        self.slider_min.setValue(0)
        self.slider_min.valueChanged.connect(self.update_max_slider)

        layout.addWidget(self.slider_max)
        layout.addWidget(self.slider_min)

        self.setLayout(layout)

    def update_min_slider(self, value):
        self.slider_min.blockSignals(True)
        self.slider_min.setValue(100 - value)
        self.slider_min.blockSignals(False)

    def update_max_slider(self, value):
        self.slider_max.blockSignals(True)
        self.slider_max.setValue(100 - value)
        self.slider_max.blockSignals(False)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = SliderWindow()
    window.show()
    sys.exit(app.exec())