要实时获取滚动条的值,可以使用以下方法:
1. 在CustomProgressBar.py中添加一个方法`getValue(self)`,用于返回当前滚动条的值:class CustomProgressBar(QProgressBar):
...
def getValue(self):
return self.value()
2. 在HuanYing.py的`showSuccessMessage`函数中,调用滚动条的`getValue`方法,判断滚动条的值是否为100,如果是则关闭HuanYing窗体,打开main窗体:def showSuccessMessage(self):
...
self.progressBar.setValue(0)
self.progressBar.show()
while self.progressBar.getValue() < 100:
pass # 等待滚动条的值达到100
# 关闭HuanYing窗体
Form.close()
# 打开main窗体
MainWindow.show()
注意,在等待滚动条的值达到100时,我们采取了一个循环来等待,该循环在滚动条的值达到100之前会一直卡住。这种方式并不是最好的实现方法,但由于您的代码结构比较复杂,其他方法可能涉及到更多的改动。
此外,代码中还有一些问题需要修正:
1. HuanYingWindow类的initUI方法中多了一个setLayout(layout)的代码,应该去掉。
2. HuanYing.py中导入的模块有缺失,需要添加:from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar, QTimer
修改后的代码如下:
HuanYing.py:from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar, QMessageBox
from PyQt5.QtGui import QIcon
from main import Ui_Form as MainUi_Form
from CustomProgressBar import CustomProgressBar
class Ui_Form(object):
def setupUi(self, Form):
...
def showSuccessMessage(self):
msgBox = QMessageBox()
msgBox.setText("登录成功!欢迎使用本报表统计系统!")
msgBox.setWindowTitle("成功")
icon = QIcon()
icon.addPixmap(QtGui.QPixmap("success.png"), QIcon.Mode.Normal, QIcon.State.Off)
msgBox.setWindowIcon(icon)
msgBox.exec()
self.progressBar.setValue(0)
self.progressBar.show()
while self.progressBar.getValue() < 100:
pass
# 关闭HuanYing窗体
Form.close()
# 打开main窗体
mainForm = QWidget()
mainUI = MainUi_Form()
mainUI.setupUi(mainForm)
mainForm.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
myForm = Ui_Form()
myForm.show()
sys.exit(app.exec())
CustomProgressBar.py:from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QProgressBar
class CustomProgressBar(QProgressBar):
def __init__(self, parent=None):
super().__init__(parent)
self.setStyleSheet(
"""
QProgressBar {
text-align: center;
color: white;
border: none;
background-color: #909090;
height: 20px;
}
QProgressBar::chunk {
background-color: #00A3E0;
}
"""
)
self.setTextVisible(True)
self.setValue(0)
def scrollToCenter(self):
max_value = self.maximum()
if max_value > 0:
self.setValue(max_value)
def startAnimation(self):
self.timer = QTimer(self)
self.timer.timeout.connect(self._animate)
self.timer.start(100)
def _animate(self):
value = self.value() + 2
if value > self.maximum():
value = value % self.maximum()
self.setValue(value)
if value == 100:
self.timer.stop()
def getValue(self):
return self.value()
class HuanYingWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QVBoxLayout(self)
self.progressBar = CustomProgressBar()
layout.addWidget(self.progressBar)
self.progressBar.scrollToCenter()
self.progressBar.startAnimation()
self.setLayout(layout)
self.showProgressBarValue()
def showProgressBarValue(self):
value = self.progressBar.getValue()
print(value)
if value < 100:
QTimer.singleShot(100, self.showProgressBarValue)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = HuanYingWindow()
window.show()
sys.exit(app.exec())
main.py:from PyQt5.QtWidgets import QApplication, QWidget
class Ui_Form(object):
def setupUi(self, Form):
...
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QWidget()
Ui_Form().setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec())
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |