鉴于此,我从PyQt5模块开始,我仍在慢慢了解其背后的逻辑。就是说,我有一个我找不到答案的问题,希望您能为我提供帮助。
我有这个脚本:
import sys, socket, time
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from io import BytesIO as by
class loadGame(QWidget):
wLoadDisplay = 768
hLoadDisplay = 576
wLoadBar = 650
hLoadBar = 40
pbarCSS = """
QProgressBar
{
font-size: 20px;
font-weight: bold;
background-color: #FFF;
border: 4px solid #000;
text-align: center;
}
QProgressBar::chunk
{
background-color: #FF0000;
width: 1px;
}
"""
labelCSS = """
QLabel
{
font-size: 20px;
font-weight: bold;
background-color: #FFF;
border: 4px solid #000;
}
"""
fileResource = []
imgResource = []
vidResource = []
audResource = []
diaResource = []
txtResource = []
internetConnection = False
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
self.outputFile = by()
self.pbar = QProgressBar(self)
self.pbar.setGeometry((self.wLoadDisplay / 2 - self.wLoadBar / 2), (self.hLoadDisplay / 2 - self.hLoadBar * 2),
self.wLoadBar, self.hLoadBar)
self.pbar.setFormat("%v/%m")
self.pbar.setValue(0)
self.pbar.setStyleSheet(self.pbarCSS)
self.label = QLabel(self)
self.label.setGeometry((self.wLoadDisplay / 2 - self.wLoadBar / 2), (self.hLoadDisplay / 2),
self.wLoadBar, self.hLoadBar)
self.label.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
self.label.setStyleSheet(self.labelCSS)
self.setGeometry(0, 0, self.wLoadDisplay, self.hLoadDisplay)
oImage = QImage("bgloading.png")
sImage = oImage.scaled(QSize(self.wLoadDisplay, self.hLoadDisplay))
palette = QPalette()
palette.setBrush(10, QBrush(sImage))
self.setPalette(palette)
qtRectangle = self.frameGeometry()
centerPoint = QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())
self.run()
def run(self):
self.checkConnection()
if self.internetConnection:
self.checkUpdate()
else:
pass
def checkConnection(self):
self.objectChange("Check Internet Connection", 1)
try:
host = socket.gethostbyname("www.google.it")
s = socket.create_connection((host, 80), 2)
self.internetConnection = True
except:
pass
self.count()
self.reset()
def checkUpdate(self):
pass
def objectChange(self, object, n):
self.label.setText(object)
self.pbar.setMaximum(n)
def count(self):
self.pbar.setValue(self.pbar.value() + 1)
def reset(self):
time.sleep(2)
self.pbar.setMaximum(0)
self.pbar.setValue(0)
self.label.setText("...")
if __name__ == '__main__':
loadDisplay = QApplication(sys.argv)
load = loadGame()
load.show()
sys.exit(loadDisplay.exec_())
在网上搜索时,我发现问题与“ time.sleep(2)”有关,也就是说,该指令阻止了直到两秒钟后才出现的窗口。
事实是,我想花一两秒钟的时间来显示该栏的完成,然后重置并继续执行“ def run(self)”中包含的下一条语句。
因此,有没有一种方法可以使该暂停而不使用“时间”模块?我不知道,也许是QTimer?我再说一遍,我对PyQt5不太了解,所以我不知道QTimer是否可以做同样的事情。
如果QTimer无法做到这一点,还有其他方法吗?我想避免使用PyQt5的“线程”,因为我读到可以做到这一点,但是我想避免仅将其用于Timer模块。
我只添加一个问题,以避免打开另一个问题并发布相同的脚本。
在脚本中,通过“ oImage = QImage(“ bgloading.png”)“等完成窗口背景。
但是,我注意到,如果发现文件名错误,或者文件本身丢失,则背景颜色为黑色。因此,如果有任何错误(名称错误或文件丢失),则可以设置背景,例如白色?
因为当窗口加载“此错误”时,不会引发任何异常并且脚本会继续。
编辑:我已经编辑了发布的脚本,使其仅包含PyQt5部分,因此您可以尝试一下。显然只有图像丢失了,可以用任何图像替换。
不幸的是,我忘记写报告“ self.set_display()”的部分是为了表明一旦PyQt5执行的工作终止,它将被关闭(仍然丢失,因为使用Pycharm会关闭执行)程序中脚本的代码)。该脚本将通过调用“ self.set_display()”函数来继续。
Edit2:我尝试按照建议替换“ time.sleep(2)”,但得到的结果相同。问题是,如果我不暂停,窗口会正常显示,但是重置发生得太快,用户看不到进度条的填充。相反,如果我输入“ time.sleep(2)”或建议的解决方案,则该窗口仅在暂停后(即已发生重置时)出现。
最佳答案
对PyQt友好的等效于time.sleep(2)
的表达式如下:
loop = QEventLoop()
QTimer.singleShot(2000, loop.quit)
loop.exec_()
造成此问题的原因是您在暂停后显示窗口小部件,必须在调用
run()
之前执行它。另一个错误是使用QImage
,对于窗口小部件的问题,您必须使用QPixmap
。如果该文件不存在,则
QPixmap
将为null,并且要知道它是否是您应该使用isNull()
方法: [...]
self.setGeometry(0, 0, self.wLoadDisplay, self.hLoadDisplay)
palette = self.palette()
pixmap = QPixmap("bgloading.png")
if not pixmap.isNull():
pixmap = pixmap.scaled(QSize(self.wLoadDisplay, self.hLoadDisplay))
palette.setBrush(QPalette.Window, QBrush(pixmap))
else:
palette.setBrush(QPalette.Window, QBrush(Qt.white))
self.setPalette(palette)
qtRectangle = self.frameGeometry()
centerPoint = QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())
self.show()
self.run()
[...]
def reset(self):
loop = QEventLoop()
QTimer.singleShot(2000, loop.quit)
loop.exec_()
self.pbar.setMaximum(0)
self.pbar.setValue(0)
self.label.setText("...")