今天是熟悉一下QSlider和QProgressBar的使用。实现如下界面:
利用Qt designer生成转化的.py代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '01.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_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(661, 576)
Form.setMinimumSize(QtCore.QSize(661, 576))
Form.setMaximumSize(QtCore.QSize(661, 576))
self.gridLayout_3 = QtWidgets.QGridLayout(Form)
self.gridLayout_3.setObjectName("gridLayout_3")
self.groupBox = QtWidgets.QGroupBox(Form)
self.groupBox.setObjectName("groupBox")
self.gridLayout_2 = QtWidgets.QGridLayout(self.groupBox)
self.gridLayout_2.setObjectName("gridLayout_2")
self.checkBox = QtWidgets.QCheckBox(self.groupBox)
self.checkBox.setObjectName("checkBox")
self.checkBox.setChecked(True)
self.gridLayout_2.addWidget(self.checkBox, 0, 0, 1, 1)
self.checkBox_2 = QtWidgets.QCheckBox(self.groupBox)
self.checkBox_2.setObjectName("checkBox_2")
self.checkBox_2.setChecked(False)
self.gridLayout_2.addWidget(self.checkBox_2, 0, 1, 1, 1)
self.radioButton = QtWidgets.QRadioButton(self.groupBox)
self.radioButton.setObjectName("radioButton")
self.gridLayout_2.addWidget(self.radioButton, 1, 0, 1, 1)
self.radioButton_2 = QtWidgets.QRadioButton(self.groupBox)
self.radioButton_2.setObjectName("radioButton_2")
self.gridLayout_2.addWidget(self.radioButton_2, 1, 1, 1, 1)
self.gridLayout_3.addWidget(self.groupBox, 1, 0, 1, 1)
self.widget = QtWidgets.QWidget(Form)
self.widget.setObjectName("widget")
self.gridLayout = QtWidgets.QGridLayout(self.widget)
self.gridLayout.setObjectName("gridLayout")
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.horizontalScrollBar = QtWidgets.QScrollBar(self.widget)
self.horizontalScrollBar.setOrientation(QtCore.Qt.Horizontal)
self.horizontalScrollBar.setObjectName("horizontalScrollBar")
self.gridLayout.addWidget(self.horizontalScrollBar, 1, 1, 1, 1)
self.label_3 = QtWidgets.QLabel(self.widget)
self.label_3.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
self.progressBar_2 = QtWidgets.QProgressBar(self.widget)
self.progressBar_2.setProperty("value", 0)
self.progressBar_2.setObjectName("progressBar_2")
self.gridLayout.addWidget(self.progressBar_2, 2, 1, 1, 1)
self.horizontalSlider = QtWidgets.QSlider(self.widget)
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.gridLayout.addWidget(self.horizontalSlider, 0, 1, 1, 1)
self.gridLayout_3.addWidget(self.widget, 0, 0, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.groupBox.setTitle(_translate("Form", "ProgressBar"))
self.checkBox.setText(_translate("Form", "textVisible"))
self.checkBox_2.setText(_translate("Form", "InvertedAppearance"))
self.radioButton.setText(_translate("Form", "显示格式--百分比"))
self.radioButton_2.setText(_translate("Form", "显示格式--当前值"))
self.label_2.setText(_translate("Form", "Scrollbar"))
self.label.setText(_translate("Form", "Slider"))
self.label_3.setText(_translate("Form", "ProgressBar"))
主文件代码如下:
from slic import Ui_Form
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5.QtCore import pyqtSlot
import sys
class QmyWidget(QWidget):
def __init__(self,parent=None):
super().__init__(parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.ui.horizontalSlider.setMaximum(200) #设置最大数值
self.ui.horizontalScrollBar.setMaximum(200)
self.ui.progressBar_2.setMaximum(200)
self.ui.horizontalSlider.valueChanged.connect(self.do_valueChanged)
self.ui.horizontalScrollBar.valueChanged.connect(self.do_valueChanged)
def on_radioButton_clicked(self): #显示格式——百分比
self.ui.progressBar_2.setFormat("%p%")
def on_radioButton_2_clicked(self): #显示格式——当前值
self.ui.progressBar_2.setFormat("%v")
@pyqtSlot(bool) #是否显示文字
def on_checkBox_clicked(self,checked):
self.ui.progressBar_2.setTextVisible(checked)
@pyqtSlot(bool) #InvertedAppearance复选框 (是否反向)
def on_checkBox_2_clicked(self,checked):
self.ui.progressBar_2.setInvertedAppearance(checked)
def do_valueChanged(self,value):
self.ui.progressBar_2.setValue(value)
if __name__ == "__main__":
app = QApplication(sys.argv)
Ui = QmyWidget()
Ui.show()
sys.exit(app.exec_())
以上功能主要使用QSlider和QProgressBar的几个属性和方法
value:QSlider和QProgressBar均有的属性,其值为QSlider或QProgressBar的数值
textVisible:为QProgressBar的方法,是否显示文字,参数为bool型,一般表示百分比的进度
format:为QProgressBar的方法,“%p%”显示百分比,“%v”显示当前值(也就是纯数字),“%m”显示总步数,一般默认为“%p%”.