time.sleep() 和背景 Windows PyQt5

time.sleep() and BackGround Windows PyQt5


鉴于此,我从 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)"有关,即指令阻塞了两秒后才出现的window
事实是,我想花一两秒钟来显示条形图的完成,然后再重置并继续包含在 "def run (self)".
中的下一个语句 那么,有没有办法在不使用 Time 模块的情况下暂停呢?我不知道,也许用 QTimer?我再说一遍,我对 PyQt5 还不是很了解,所以我不知道 QTimer 是否可以做同样的事情。
如果 QTimer 做不到,还有其他办法吗?我想避免使用 PyQt5 的 "threads",因为我读到可以这样做,但我想避免仅将它用于 Timer 模块。


我只添加一个问题,以避免打开另一个问题并发布相同的脚本。
在脚本中,window后台是通过"oImage = QImage ("bgloading.png")"等方式完成的
但是,我注意到,如果发现文件名错误,或者文件本身丢失,背景就会变成黑色。所以,如果有任何错误(错误的名称或丢失的文件),可以设置背景,例如白色?
因为当 window 加载 "this error" 时,不会引发异常并且脚本继续。



编辑:我编辑了已发布的脚本,使其仅包含 PyQt5 部分,因此您可以尝试一下。很明显只是缺少图片,可以用任何图片替换。
不幸的是我忘了写 "self.set_display ()" 被报告的部分是为了表明一旦 PyQt5 执行的工作被终止,它就会被关闭(这仍然是缺失的,因为使用 Pycharm 我会关闭从程序执行脚本)。该脚本将通过调用 "self.set_display ()" 函数继续。

Edit2:我按照建议尝试替换 "time.sleep (2)",但我得到了相同的结果。问题是,如果我不暂停, window 会正常出现,但重置发生得太快,用户看不到进度条的填充。相反,如果我输入 "time.sleep (2)" 或建议的解决方案,则 window 仅在暂停后出现,即重置已经发生时。

等价于time.sleep(2)的对PyQt友好的表达式如下:

loop = QEventLoop()
QTimer.singleShot(2000, loop.quit)
loop.exec_()

这个问题是因为你在暂停后显示小部件,你必须在调用run()之前这样做,还有一个错误是使用QImage,对于你必须使用的小部件的问题QPixmap.

如果文件不存在,那么 QPixmap 将为空,要知道它是否存在,您应该使用 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("...")