Skip to main content
 首页 » 编程设计

python之time.sleep() 和 BackGround Windows PyQt5

2024年09月07日7exmyth


鉴于此,我从 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)”中包含的下一条语句。
那么,有没有办法在不使用 Time 模块的情况下暂停呢?我不知道,也许用 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 将为空,要知道它是否存在,您应该使用 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("...")