简体   繁体   English

PyQt运行时问题

[英]PyQt run time issue

I want my code to run by showing the qtwidget and then running the forloop, but it runs the forloop then shows my widget to me. 我希望通过显示qtwidget然后运行forloop来运行我的代码,但它会运行forloop然后向我显示我的小部件。 Why is this? 为什么是这样?

class tes(QWidget):

    def __init__(self):
        super(tes, self).__init__()
        self.initUI()
        for i in range (1000000):
            print("s")

    def initUI(self):
        t = QTableWidget(8,8,self)        
        self.show()
        self.resize(1000,1000)
        t.setGeometry(0,0,500,500)
        t.show()

def main():
    app = QApplication(sys.argv)
    t = tes()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

Add QApplication.processEvents() before loop. 在循环之前添加QApplication.processEvents() Your widget will be shown, but unresponsive. 您的小部件将显示,但没有响应。 To make application responsive, add processEvents() calls to some steps of your loop. 为了使应用程序具有响应能力,请将processEvents()调用添加到循环的某些步骤。

Example: 例:

def __init__(self):
    super(tes, self).__init__()
    self.initUI()
    QApplication.processEvents()
    for i in range (1000000):
        if not i % 3:  # let application process events each 3 steps.
            QApplication.processEvents()
        print("s")

这是因为在tes对象初始化期间执行了for循环之后运行了app.exec_()

The widget is only shown once the application is running, not when its initialised. 该窗口小部件仅在应用程序运行时显示,而在初始化时不显示。 What exactly are you trying to do in the loop? 您到底想在循环中做什么? It might be better to connect it to a signal or handle it in an event, but it all depends what you're trying to acheive. 将其连接到信号或在事件中处理它可能会更好,但这完全取决于您要实现的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM