python - 如何在PyQt中启动 "drawing loop"?

标签 python pyqt

通常,当我们绘制 GUI 时,我们希望 GUI 根据程序中的数据变化进行更新。在程序开始时,假设我已经根据初始数据绘制了 GUI。这些数据会不断变化,那么我如何不断地重绘我的 GUI?

最佳答案

我发现做到这一点的最好方法是在 QThread 中运行核心程序并使用信号与 GUI 进行通信。这是一个示例,其中我在主程序执行一些操作时更新进度对话框。

这是我正在开发的一个项目的代码摘录。基本思想是,我将许多文件添加到库对象中,并在添加文件时更新进度。

该操作由Library 类启动。执行实际工作的步骤位于 AddFilesThread 中。

请告诉我这是否有帮助。如果您需要,我可以尝试整理一个工作示例而不是代码摘录。



如果您想查看我使用的完整代码,请转到此处:hystrix_library.py 。我使用的diaglog 类位于该文件中。我不能说这一定是最好的做事方式,但它效果很好并且相当容易阅读。

class Library(QtCore.QObject):
    """
    This class is used to store information on the libraries.
    """
    def __init__(self):
        QtCore.QObject.__init__(self)

    def importUrls(self, url_list):

        # Create a progress dialog
        self.ui_progress = AddUrlsProgressDialog()
        self.ui_progress.show()
        self.ui_progress.raise_()

        # Create the add files thread object.
        self.add_files_thread = AddFilesThread()

        # Connect the thread to the dialog.
        self.connect(self.add_files_thread
                     ,QtCore.SIGNAL('updateDialog')
                     ,self.ui_progress.setDialog)

        self.connect(self.add_files_thread
                     ,QtCore.SIGNAL('updateValue')
                     ,self.ui_progress.setValue)

        self.connect(self.add_files_thread
                     ,QtCore.SIGNAL('finished')
                     ,self.ui_progress.setFinished)

        self.connect(self.add_files_thread
                     ,QtCore.SIGNAL('canceled')
                     ,self.ui_progress.closeNow)

        # Connect the dialog to the thread
        self.connect(self.ui_progress
                     ,QtCore.SIGNAL('cancel')
                     ,self.add_files_thread.cancelRequest)        

        # Start the thread
        self.add_files_thread.start()




class AddFilesThread(QtCore.QThread):

    def __init__(self, parent=None):
        QtCore.QThread.__init__(self, parent)

        self.cancel_request = False

    def __del__(self):
        self.wait()

    def run(self):
        try:
            self.main()
        except:
            print 'AddFilesThread broke yo.'
            self.cancelNow(force=True)
            traceback.print_exc()

    def main(self):
        num_added = 0
        for local_path in self.path_list:
            # First Setup the dialog
            status_label = 'Finding files to add . . .'
            dialog_update = (status_label, (0,0), 0)
            self.emit(QtCore.SIGNAL('updateDialog'), dialog_update)

            # Do a recursive search.
            all_files = hystrix_file.getFiles()
            num_files = len(all_files)

            if self.cancelNow():
                return

            status_label = '%d files found.\nExtracting tags . . .' %(num_files)
            dialog_update = (status_label, (0,num_files), 0)
            self.emit(QtCore.SIGNAL('updateDialog'), dialog_update)

            num_added = 0
            for index, filename in enumerate(all_files):
                try:
                    metadata = hystrix_tags.getMetadata(filename)
                    # Here I would add the metadata to my library.
                except:
                    traceback.print_exc()
                    print('Could not extract Metadata from file.')
                    continue

                # This should be sent to a progress widget
                if index % 1 == 0:
                    self.emit(QtCore.SIGNAL('updateValue'), index)

                # Check if a cancel signal has been recieved
                if self.cancelNow():
                    return

        status_label = 'Finished. Added %d files.' %(num_added)
        dialog_update = ( status_label, (0,num_added), num_added)
        self.emit(QtCore.SIGNAL('updateDialog'), dialog_update)

        self.emit(QtCore.SIGNAL('finished'))

    def cancelRequest(self):
        self.cancel_request = True

    def cancelNow(self, force=False):
        if self.cancel_request or force:
            self.emit(QtCore.SIGNAL('canceled'))
            return True
        else:
            return False

关于python - 如何在PyQt中启动 "drawing loop"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2192993/

相关文章:

条件生成器表达式返回 True 的 Python "all"函数。为什么?

javascript - Python Selenium : Unable to click button

python - 我试图以 headless (headless)模式打开一个站点,但它似乎陷入了一个激进的循环,我该如何解决?

使用 QT Designer 的 QT/PyQT 最佳实践

python - PyQt 从 QtDesigner 生成的 MainWindow 打开一个新的 Windowed Widget

python - 如何在python中插入图像

python - 如何在调试日志中查看 Python 请求使用哪个 IP 地址进行连接?

python - 使用 cx_Freeze 卡住时主窗口图标不显示

qt - 如何创建将QAbstractItemModel的节点展平到PySide中的列表的代理模型?

python - PyQt、QThread、GIL、图形用户界面