简体   繁体   English

如何显示PySide / PyQt UI并自动运行其中一种方法?

[英]How to show PySide/PyQt UI and auto-run one of its methods?

I wish to open a PySide/PyQt window and automatically start executing a method, which will show a progress in the UI while it is executing. 我希望打开一个PySide / PyQt窗口并自动开始执行一个方法,该方法将在UI执行时显示进度。

With the code below, the ui is not shown until the process has completed, thus you cannot see the progress. 使用下面的代码,直到过程完成才会显示ui,因此您无法看到进度。 How can I change the code to see the progress before the process has completed? 如何在流程完成之前更改代码以查看进度?

from PySide import QtGui
import time

class MyApp(QtGui.QMainWindow)
    def __init__(self, parent=None):
        super(MyApp, self).__init__(parent)

        # Setup
        self.centralWidget = QtGui.QWidget(self)
        self.setCentralWidget(self.centralWidget)
        self.setup_UI()

        # Execute process!
        self.process()

    def setup_UI(self):
        ''' Attach widgets to window '''
        self.mainLayout=QtGui.QVBoxLayout(self.centralWidget)
        self.list_widget = QtGui.QListWidget()
        self.progress_bar = QtGui.QProgressBar()
        self.mainLayout.addWidget(self.list_widget)
        self.mainLayout.addWidget(self.progress_bar)

    def process(self):
        ''' Manipulate the ui '''
        self.progress_bar.setMaximum(0)
        self.progress_bar.setMaximum(10)

        for x in range(0, 10):
            time.sleep(1)
            self.list_widget.addItem('Item ' + str(x))
            self.progress_bar.setValue(x)



my_app = MyApp()
my_app.show()

Your main problem that you are blocking qt main thread by calling time.sleep . 你通过调用time.sleep来阻止qt主线程的主要问题。 To solve this issue you have two options. 要解决此问题,您有两种选择。 One of them is using threading . 其中一个是使用线程 Another option is change your code to asynchronous like this: 另一种选择是将代码更改为异步,如下所示:

from PySide import QtGui, QtCore
import time
import sys

class MyApp(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MyApp, self).__init__(parent)

        # Setup
        self.centralWidget = QtGui.QWidget(self)
        self.setCentralWidget(self.centralWidget)
        self.setup_UI()

        # Execute process!
        self.set_process()
        self.timer = QtCore.QTimer()
        self.i = 0
        self.timer.timeout.connect(self.update)
        self.timer.start(1000)

    def setup_UI(self):
        ''' Attach widgets to window '''
        self.mainLayout=QtGui.QVBoxLayout(self.centralWidget)
        self.list_widget = QtGui.QListWidget()
        self.progress_bar = QtGui.QProgressBar()
        self.mainLayout.addWidget(self.list_widget)
        self.mainLayout.addWidget(self.progress_bar)

    def set_process(self):
        ''' Manipulate the ui '''
        self.progress_bar.setMaximum(0)
        self.progress_bar.setMaximum(10)

    def update(self):
        if self.i > 9:
            self.timer.stop()

        self.list_widget.addItem('Item ' + str(self.i))
        self.progress_bar.setValue(self.i)
        self.i += 1


def main():
    app = QtGui.QApplication(sys.argv)
    my_win = MyApp()
    my_win.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

This example are using Qtimer object for updating progress bar with delay. 此示例使用Qtimer对象来更新具有延迟的进度条。

The example can be made to work quite easily by starting the processing with a single-shot timer and then calling processEvents within the loop to update the GUI: 通过使用单次定时器启动处理,然后在循环内调用processEvents来更新GUI,可以非常轻松地使用该示例:

    # Execute process!
    QtCore.QTimer.singleShot(100, self.process)

    ...

    for x in range(0, 10):
        time.sleep(1)
        ...
        QtGui.qApp.processEvents(QtCore.QEventLoop.AllEvents, 50)

However, there is no guarantee that this type of approach will work with a more realistic example. 但是,不能保证这种方法可以用更实际的例子。 You may end up needing to use threads or multiprocessing - it all depends on the specific kind of processing you are going to do. 您可能最终需要使用线程或多处理 - 这一切都取决于您要执行的特定处理类型。

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

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