PyQt5 series of tutorials (thirty-one) use of sliders

Software and hardware environment

  • Windows 10 64bit
  • Anaconda3 with python 3.8
  • PyQt5 5.15

foreword

Slider QSlider is also a common control, which is divided into horizontal slider and vertical slider. It is usually used to obtain user input, usually numerical value, which is simple and intuitive.

Practical

Some comments are written in the code

 import sys from PyQt5.QtWidgets import QMainWindow, QApplication, QSlider from ui import Ui_MainWindow class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) # 水平滑动条# 设置最小值self.horizontalSlider.setMinimum(0) # 设置最大值self.horizontalSlider.setMaximum(100) # 默认值,这里放一个label 控件来显示当前滑动条的值self.label_horizontal.setText('50') # 步长,比如水平滑动条使用方向键一次,滑动条增加/减少的值self.horizontalSlider.setSingleStep(2) # 刻度显示的位置,不添加这句,即不设置刻度self.horizontalSlider.setTickPosition(QSlider.TicksBelow) # valueChanged信号的绑定,常见的信号还有sliderPressed、sliderReleased、sliderMoved self.horizontalSlider.valueChanged.connect(self.update_horizontal) # 垂直滑动条的操作非常类似self.verticalSlider.setMinimum(0) self.verticalSlider.setMaximum(1000) self.label_vertical.setText('500') self.verticalSlider.setSingleStep(10) self.verticalSlider.setTickPosition(QSlider.TicksRight) self.verticalSlider.valueChanged.connect(self.update_vertical) def update_horizontal(self): self.label_horizontal.setText(str(self.horizontalSlider.value())) def update_vertical(self): self.label_vertical.setText(str(self.verticalSlider.value())) if __name__ == '__main__': app = QApplication(sys.argv) windows = MainWindow() windows.show() sys.exit(app.exec_())

Source code download

https://github.com/xugaoxiang/learningPyQt5

PyQt5 series of tutorials

For more PyQt5 tutorials, please move

https://xugaoxiang.com/category/python/pyqt5/

This article is reprinted from https://xugaoxiang.com/2022/09/15/pyqt5-31-qslider/
This site is for inclusion only, and the copyright belongs to the original author.