想用pyqt5做一个显示脚本运行的进度条,但是不知道如何写代码才能触发qprogressbar控件的信号槽监测到我的这个脚本
我的脚本文件:
import os,sys
def file_calcuation3(file3):
for foldername in os.listdir(file3):
print(foldername)
old_file_name = foldername
newname = foldername.replace('1_', '')
print(newname)
new_file_name = newname
os.rename(file3+'/' + old_file_name,file3+'/' + new_file_name)
上网查大都数案例都是下面这种,请问我该如何进行代码编写,才能让Qprogressbar控件知道我的脚本运行结果,实现我的脚本文件的进度显示
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QProgressBar
from PyQt5.QtCore import QBasicTimer
class MyClass(QWidget):
definit(self):
super().init()
self.initUI()
def initUI(self):
self.resize(300, 200)
载入进度条控件
self.pgb = QProgressBar(self)
self.pgb.move(50, 50)
self.pgb.resize(250, 20)
self.pv = 0
self.timer1 = QBasicTimer()
self.pgb.setMinimum(0)
self.pgb.setMaximum(100)
self.pgb.setValue(self.pv)
self.btn = QPushButton("开始", self)
self.btn.move(50, 100)
self.btn.clicked.connect(self.myTimerState)
self.show()
def myTimerState(self):
if self.timer1.isActive():
self.timer1.stop()
self.btn.setText("开始")
else:
self.timer1.start(100, self)
self.btn.setText("停止")
def timerEvent(self, e):
if self.pv == 100:
self.timer1.stop()
self.btn.setText("完成")
else:
self.pv += 1
self.pgb.setValue(self.pv)
if name == "main":
app = QApplication(sys.argv)
mc = MyClass()
app.exec_()