非常抱歉给您带来困扰。以下是一个更优雅的解决方案,使用QTimer来实现异步操作:
在HuanYing.py中,为HuanYingWindow类添加一个新的槽函数onProgressBarFinished,用于处理滚动条值达到100%时的操作:
class HuanYingWindow(QWidget):
...
@QtCore.pyqtSlot()
def onProgressBarFinished(self):
# 关闭HuanYing窗体
self.close()
# 打开MainWindow窗体
MainWindow.show()
在showSuccessMessage方法中,连接滚动条的valueChanged信号到HuanYingWindow的onProgressBarFinished槽函数,并将滚动条的值设为0:
def showSuccessMessage(self):
...
self.progressBar.setValue(0)
self.progressBar.valueChanged.connect(self.onProgressBarFinished)
self.progressBar.show()
在CustomProgressBar.py的_startAnimation方法中,判断如果滚动条的值达到100%,则发射一个自定义的progressFinished信号:
def _animate(self):
value = self.value() + 2
if value > self.maximum():
value = value % self.maximum()
self.setValue(value)
if value == 100:
self.timer.stop()
self.progressFinished.emit()
在HuanYing.py的HuanYingWindow类的initUI方法中,连接滚动条的progressFinished信号到HuanYingWindow的onProgressBarFinished槽函数:
def initUI(self):
layout = QVBoxLayout(self)
self.progressBar = CustomProgressBar()
layout.addWidget(self.progressBar)
self.progressBar.scrollToCenter()
self.progressBar.startAnimation()
self.progressBar.progressFinished.connect(self.onProgressBarFinished)
self.setLayout(layout)
self.showProgressBarValue()
最后,在main.py中创建MainWindow对象之前连接HuanYingWindow的onProgressBarFinished方法到MainWindow的show槽函数:
from PyQt5.QtCore import QApplication, QTimer
from HuanYing import Ui_Form as HuanYingUi_Form, Signal
class Ui_Form(object):
def setupUi(self, Form):
...
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QWidget()
ui = Ui_Form()
ui.setupUi(MainWindow)
huanYingWindow = HuanYingUi_Form()
huanYingWindow.showSuccessMessage()
huanYingWindow.onProgressBarFinished.connect(MainWindow.show)
sys.exit(app.exec())
现在,当滚动条的值达到100%时,会发射一个自定义的progressFinished信号,在HuanYingWindow的槽函数中处理关闭HuanYing窗体和打开MainWindow窗体的操作。这种方式避免了阻塞主线程,并且代码更加优雅。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |