凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

24. PyQt6 QSlider

2023-10-17 python 315
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QSlider, QLabel
from PyQt6.QtGui import QGuiApplication, QPixmap
from PyQt6.QtCore import Qt


class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.lbl = None
        self.initUI()

    def initUI(self):
        self.setWindowTitle("PyQt6 QSlider")

        sld = QSlider(Qt.Orientation.Horizontal, self)
        sld.setFocusPolicy(Qt.FocusPolicy.NoFocus)
        sld.setGeometry(30, 40, 200, 30)
        sld.valueChanged[int].connect(self.changeValue)

        self.lbl = QLabel(self)
        self.lbl.setPixmap(QPixmap("mute.png"))
        self.setGeometry(250, 40, 80, 30)

        self.setGeometry(100, 100, 400, 300)
        self.center()

    def changeValue(self, value):

        print(value)
        if value == 0:
            self.lbl.setPixmap(QPixmap("mute.png"))
        elif 0 < value <= 30:
            self.lbl.setPixmap(QPixmap("mute.png"))
        elif 30 < value < 80:
            self.lbl.setPixmap(QPixmap("mute.png"))
        else:
            self.lbl.setPixmap(QPixmap("mute.png"))

    def center(self):
        qr = self.frameGeometry()
        cp = QGuiApplication.primaryScreen().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


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


文章评论

0条评论