任务目标:
实现跑马灯效果,单击开始,进度条开始动,单击停止,进度条停止
设计:
完整代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(688, 533)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.progressBar_1 = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar_1.setGeometry(QtCore.QRect(160, 30, 400, 60))
self.progressBar_1.setMaximum(100)#这个设置成100,在开始运行的时候才不会动
self.progressBar_1.setProperty("value", -1)
self.progressBar_1.setOrientation(QtCore.Qt.Horizontal)
self.progressBar_1.setObjectName("progressBar")
self.progressBar_3 = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar_3.setGeometry(QtCore.QRect(120, 370, 401, 60))
self.progressBar_3.setMaximum(100)
self.progressBar_3.setLayoutDirection(QtCore.Qt.RightToLeft)
self.progressBar_3.setProperty("value", -1)
self.progressBar_3.setTextDirection(QtWidgets.QProgressBar.TopToBottom)
self.progressBar_3.setObjectName("progressBar_3")
self.progressBar_4 = QtWidgets.QProgressBar(self.
centralwidget)
self.progressBar_4.setGeometry(QtCore.QRect(100, 30, 60, 400))
self.progressBar_4.setMaximum(100)
self.progressBar_4.setProperty("value", -1)
self.progressBar_4.setOrientation(QtCore.Qt.Vertical)
self.progressBar_4.setObjectName("progressBar_4")
self.progressBar_2 = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar_2.setGeometry(QtCore.QRect(520, 30, 61, 401))
self.progressBar_2.setMaximum(100)
self.progressBar_2.setProperty("value", -1)
self.progressBar_2.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.progressBar_2.setOrientation(QtCore.Qt.Vertical)
self.progressBar_2.setInvertedAppearance(True)
self.progressBar_2.setObjectName("progressBar_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(290, 230, 93, 28))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 688, 29))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.timer = QtCore.QBasicTimer() # 创建定时器对象
# 为按钮绑定单击信号
self.pushButton.clicked.connect(self.running)
# 控制进度条的滚动效果
def running(self):
if self.timer.isActive(): # 判断计时器是否开启
self.timer.stop() # 停止计时器
self.pushButton.setText('开始')# 设置按钮的文本
# 设置4个进度条的最大值为100
self.progressBar_1.setMaximum(100)
self.progressBar_2.setMaximum(100)
self.progressBar_3.setMaximum(100)
self.progressBar_4.setMaximum(100)
else:
self.timer.start(100,MainWindow) # 启动计时器
self.pushButton.setText('停止') # 设置按钮的文本
# 将4个进度条的最大值和最小值都设置为0,以便显示循环滚动的效果
self.progressBar_1.setMinimum(0)
self.progressBar_1.setMaximum(0)
self.progressBar_2.setMinimum(0)
self.progressBar_2.setMaximum(0)
self.progressBar_3.setMinimum(0)
self.progressBar_3.setMaximum(0)
self.progressBar_4.setMinimum(0)
self.progressBar_4.setMaximum(0)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "跑马灯效果"))
self.pushButton.setText(_translate("MainWindow", "开始"))
import sys
if __name__ == '__main__':
app=QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
效果: