datetime:2019/5/21 15:44
author:nzb

创建滑动条

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QGroupBox, QCheckBox, \
    QHBoxLayout, QPushButton, QButtonGroup, QFrame, QSlider
from PyQt5.QtWidgets import QSplitter, QLineEdit
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import Qt


class UI_demo(QWidget):
    """用户界面"""
    def __init__(self):
        super().__init__()

        # 窗口信息
        self.title = 'PyQt5 Slider'
        self.left = 600
        self.top = 200
        self.width = 500
        self.height = 500
        self.iconName = '../img/home.ico'

        self.initWindow()

    def initWindow(self):

        # 窗口信息
        self.setWindowIcon(QtGui.QIcon(self.iconName))  # 图标设置
        self.setGeometry(self.left, self.top, self.width, self.height)  # 大小位置设置
        self.setWindowTitle(self.title)  # 窗口标题
        self.setStyleSheet('background-color:yellow')

        # 滑动条
        hbox = QHBoxLayout()  # 水平布局

        self.slider = QSlider()
        self.slider.setOrientation(Qt.Horizontal)  # 水平滑动(默认垂直)
        self.slider.setTickPosition(QSlider.TicksBelow)  # 刻度
        self.slider.setTickInterval(10)  # 设置刻度数量
        self.slider.setMinimum(0)         # 滑动条最小值
        self.slider.setMaximum(100)         # 滑动条最大值
        self.slider.valueChanged.connect(self.changedValue)  # 绑定事件

        self.label = QLabel('0')
        self.label.setFont(QtGui.QFont('Sanserif', 15))

        hbox.addWidget(self.slider)
        hbox.addWidget(self.label)

        self.setLayout(hbox)

        # 展示窗口
        self.show()

    def changedValue(self):
        size = self.slider.value()
        self.label.setText(str(size))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = UI_demo()
    sys.exit(app.exec_())

results matching ""

    No results matching ""