PyQt5:在运行时更新标签 [英] PyQt5: Update labels inrun time

查看:150
本文介绍了PyQt5:在运行时更新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时在循环中更新标签时遇到问题.我想我需要使用信号等等,但我已经尝试了我现在能想到的一切.我想让我的程序做什么:

I have a problem updating labels in a loop during run time. I think I need to use signals and so on, but I've tried everything I can think of now. What I want my program to do:

当我点击按钮时,一个循环应该开始运行一些需要一些时间的函数.当函数正在运行时,相应的标签应更新其文本以显示正在运行",完成后应显示完成"并继续下一个函数.我已经创建了一些代表我所追求的虚拟代码!

When I click the button a loop should start that run some function that take some time. While the function is running the corresponding label should update its text to say "running" and when its done it should say "done" and continue to the next function. I've created some dummy code that represent what I'm after!

我用一个只需要一些时间的虚拟函数来表示我的函数.

I have represented my function with a dummy function that just take some time.

import sys
from PyQt5.QtWidgets import *
import time

class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):
        # Set up an example ui
        qbtn = QPushButton('Click', self)
        qbtn.clicked.connect(self.changeLabels)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(100, 50)
        # Add labels
        self.labels = list()
        self.labels.append(QLabel("Lbl1", self))
        self.labels[-1].setFixedSize(50, 20)
        self.labels.append(QLabel("Lbl2", self))
        self.labels[-1].setFixedSize(50, 20)
        self.labels[-1].move(0, 20)
        self.labels.append(QLabel("Lbl3", self))
        self.labels[-1].setFixedSize(50, 20)
        self.labels[-1].move(0, 40)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Test')
        self.show()

    def changeLabels(self):
        # Loop over all labels. For each label a function will be executed that will take some time. In this case
        # I represent that with a dummy function to just take time. While the function is running the label should say
        # "running" and when its finished it should say "done".
        for lbl in self.labels:
            orgTxt = lbl.text()
            lbl.setText("%s Running" % orgTxt)
            self.dummyFunction()
            lbl.setText("%s Done" % orgTxt)

    def dummyFunction(self):
        time.sleep(1)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

推荐答案

GUI 不支持阻塞任务,因为 GUI 需要一些时间来更新窗口的某些属性,一个可能的解决方案是使用新线程并实现dummy 函数,在下面的示例中,使用信号显示了实现.如果您的实际函数更新了任何 GUI 视图,则不应直接在线程中进行,而应通过信号进行.

The GUI does not support blocking tasks because the GUI needs some time to update some attributes of the window, a possible solution is to use a new thread and implement the dummy function, in the following example the implementation is shown with the use of signals. If your real function updates any GUI view you should not do it directly in the thread, you should do it through signals.

class DummyThread(QThread):
    finished = pyqtSignal()
    def run(self):
        time.sleep(1)
        self.finished.emit()

class Example(QWidget):
    [...]
    def changeLabels(self):
        for lbl in self.labels:
            orgTxt = lbl.text()
            lbl.setText("%s Running" % orgTxt)
            thread = DummyThread(self)
            thread.start()
            thread.finished.connect(lambda txt=orgTxt, lbl=lbl : lbl.setText("%s Done" % txt))

这篇关于PyQt5:在运行时更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆