一、QSlider和QprogressBar的组合
QSlider / QScrollBar
在Input Widgets分组中,QSlider和QScrollBar是两个滑动型输入组件
QSlider和ScrollBar都是从QAbstractSlider类继承来的,拥有一些相同的属性,通过属性编辑器可以查看效果。
属性 | 含义 |
---|---|
minimum、maximum | 输入范围的最小值和最大值 |
singleStep | 单步长,拖动标尺上的滑块,或按下左、右键时的最小变化数值 |
pageStep | 按PgUp或PgDn键时变化的数值 |
value | 当前值,拖动滑块时自动改变此值 |
sliderPosition | 滑块的位置,若tracking属性设置为True,sliderPosition就等于value |
tracking | sliderPosition是否等同于value |
orientation | Slider、ScrollBar的方向,可以设置为水平或垂直 |
invertedAppearance | 显示方向是否为反方向 |
invertedControls | 反向按键控制 |
QprogressBar
在Display Widgets分组中,QprogressBar是进度条显示组件
QprogressBar的父类是QWidget,一般用于进度显示,常用的属性如下:
属性 | 含义 |
---|---|
minimum、maximum | 输入范围的最小值和最大值 |
value | 当前值,可以设定或读取当前值 |
textVisible | 是否显示文字,文字一般是百分比表示进度 |
orientation | 可以设置为水平或垂直 |
format | 显示文字的格式,“%p%”显示百分比,“%v”显示当前值,默认为“%p%” |
完整代码
ui_Widget.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui_Widget.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_proBar(object):
def setupUi(self, proBar):
proBar.setObjectName("proBar")
proBar.resize(400, 300)
self.horizontalSlider = QtWidgets.QSlider(proBar)
self.horizontalSlider.setGeometry(QtCore.QRect(60, 40, 171, 16))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.horizontalScrollBar = QtWidgets.QScrollBar(proBar)
self.horizontalScrollBar.setGeometry(QtCore.QRect(50, 80, 181, 16))
self.horizontalScrollBar.setOrientation(QtCore.Qt.Horizontal)
self.horizontalScrollBar.setObjectName("horizontalScrollBar")
self.progressBar = QtWidgets.QProgressBar(proBar)
self.progressBar.setGeometry(QtCore.QRect(60, 130, 201, 23))
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.retranslateUi(proBar)
QtCore.QMetaObject.connectSlotsByName(proBar)
def retranslateUi(self, proBar):
_translate = QtCore.QCoreApplication.translate
proBar.setWindowTitle(_translate("proBar", "Form"))
ui_Widget_main.py
# -*- coding: utf-8 -*-
# @Time : 2022/9/27 9:47
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPixmap
from ui_Widget import Ui_proBar
class QmyWidget(QWidget):
def __init__(self, parent = None):
super().__init__(parent)
self.ui = Ui_proBar()
self.ui.setupUi(self)
self.ui.horizontalSlider.setMaximum(100)
self.ui.horizontalScrollBar.setMaximum(100)
self.ui.progressBar.setMaximum(100)
#将滑块/滑动条的 valueChanged 信号连接到 do_valueChanged 方法(槽)
self.ui.horizontalSlider.valueChanged.connect(self.do_valueChanged)
self.ui.horizontalScrollBar.valueChanged.connect(self.do_valueChanged)
#自定义槽函数 do_valueChanged(),根据传递来的参数 value ,设置为 progressBar 的当前值
def do_valueChanged(self, value):
self.ui.progressBar.setValue(value)
if __name__ == "__main__":
app = QApplication(sys.argv)
proBar = QmyWidget()
proBar.show()
sys.exit(app.exec_())
运行结果
二、QSlider和QPixmap的组合
完整代码
ui_927.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui_927.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_proBar(object):
def setupUi(self, proBar):
proBar.setObjectName("proBar")
proBar.resize(400, 300)
self.horizontalSlider = QtWidgets.QSlider(proBar)
self.horizontalSlider.setGeometry(QtCore.QRect(60, 40, 171, 16))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.label = QtWidgets.QLabel(proBar)
self.label.setGeometry(QtCore.QRect(240, 10, 151, 111))
self.label.setObjectName("label")
self.retranslateUi(proBar)
QtCore.QMetaObject.connectSlotsByName(proBar)
def retranslateUi(self, proBar):
_translate = QtCore.QCoreApplication.translate
proBar.setWindowTitle(_translate("proBar", "Form"))
self.label.setText(_translate("proBar", "picture"))
ui_927_main.py文章来源:https://www.toymoban.com/news/detail-515522.html
# -*- coding: utf-8 -*-
# @Time : 2022/9/27 9:47
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPixmap
from ui_927 import Ui_proBar
class QmyWidget(QWidget):
def __init__(self, parent = None):
super().__init__(parent)
self.ui = Ui_proBar()
self.ui.setupUi(self)
self.ui.horizontalSlider.setMaximum(100)
self.ui.label.setPixmap(QPixmap("./images/01.png"))
self.ui.horizontalSlider.valueChanged.connect(self.do_valueChanged)
def do_valueChanged(self, value):
if value < 30:
self.ui.label.setPixmap(QPixmap("./images/02.png"))
elif 30 <= value < 60:
self.ui.label.setPixmap(QPixmap("./images/03.png"))
else:
self.ui.label.setPixmap(QPixmap("./images/04.png"))
if __name__ == "__main__":
app = QApplication(sys.argv)
proBar = QmyWidget()
proBar.show()
sys.exit(app.exec_())
运行结果
文章来源地址https://www.toymoban.com/news/detail-515522.html
到了这里,关于PyQt5之滑块及滑动条控件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!