PyQt基础_009_ 按钮类控件QSlider

基本功能

PyQt基础_009_ 按钮类控件QSlider_第1张图片

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class SliderDemo(QWidget):
    def __init__(self, parent=None):
        super(SliderDemo, self).__init__(parent)
        self.setWindowTitle("QSlider 例子") 
        self.resize(300, 100)

        layout = QVBoxLayout()
        self.l1 = QLabel("Hello World")
        self.l1.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.l1)
        # 水平方向
        self.sl = QSlider(Qt.Horizontal)
        #设置最小值
        self.sl.setMinimum(10)
        #设置最大值
        self.sl.setMaximum(50)
        # 步长
        self.sl.setSingleStep( 3 ) 
        # 设置当前值
        self.sl.setValue(20)
        # 刻度位置,刻度在下方
        self.sl.setTickPosition(QSlider.TicksBelow)
        # 设置刻度间隔
        self.sl.setTickInterval(5)
        layout.addWidget(self.sl)
        # 连接信号槽
        self.sl.valueChanged.connect(self.valuechange)
        self.setLayout(layout)

    def valuechange(self):
        print('current slider value=%s' % self.sl.value() )
        size = self.sl.value()
        self.l1.setFont(QFont("Arial",size))
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = SliderDemo()
    demo.show()
    sys.exit(app.exec_())

跟着滑块显示数值

PyQt基础_009_ 按钮类控件QSlider_第2张图片

import sys

from PyQt5.Qt import *

class MSlider(QSlider):
    def __init__(self, parent=None, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.setTickPosition(QSlider.TicksBothSides)
        self.setup_ui()

    def setup_ui(self):
        self.label = QLabel(self)
        self.label.setText("0")
        self.label.setStyleSheet("color: red;")
        self.label.hide()

    def get_label_pos(self) -> (int, int):
        """计算label应该处于的位置"""
        x = int((self.width() - self.label.width()) / 2)
        y = int(
                (1 - self.value() / (self.maximum() - self.minimum()))
                * (self.height() - self.label.height())
        )
        return x, y

    def mousePressEvent(self, ev) -> None:
        super(MSlider, self).mousePressEvent(ev)
        self.label.move(*self.get_label_pos())
        self.label.show()
        self.label.setText(str(self.value()))
        self.label.adjustSize()

    def mouseMoveEvent(self, ev) -> None:
        super(MSlider, self).mouseMoveEvent(ev)
        self.label.move(*self.get_label_pos())
        self.label.setText(str(self.value()))
        self.label.adjustSize()

    def mouseReleaseEvent(self, ev) -> None:
        super(MSlider, self).mouseReleaseEvent(ev)
        self.label.hide()

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QSlider-拓展案例")
        self.resize(500, 500)
        self.setup_ui()

    def setup_ui(self):
        # 0.创建两个控件
        slider = MSlider(self)
        slider.move(200, 200)
        slider.resize(30, 200)

if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())

你可能感兴趣的:(PyQt,pyqt)