簡體   English   中英

如何顯示PySide / PyQt UI並自動運行其中一種方法?

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

我希望打開一個PySide / PyQt窗口並自動開始執行一個方法,該方法將在UI執行時顯示進度。

使用下面的代碼,直到過程完成才會顯示ui,因此您無法看到進度。 如何在流程完成之前更改代碼以查看進度?

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()

你通過調用time.sleep來阻止qt主線程的主要問題。 要解決此問題,您有兩種選擇。 其中一個是使用線程 另一種選擇是將代碼更改為異步,如下所示:

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()

此示例使用Qtimer對象來更新具有延遲的進度條。

通過使用單次定時器啟動處理,然后在循環內調用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)

但是,不能保證這種方法可以用更實際的例子。 您可能最終需要使用線程或多處理 - 這一切都取決於您要執行的特定處理類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM