PyQt处理导致GUI滞后

时间:2011-07-26 13:27:31

标签: python user-interface pyqt

我正在使用python编写一个应用程序,它将处理一个txt文件。用户从硬盘驱动器中选择一个文件,程序将打开并处理它。我遇到的主要问题是,你第一次运行它,它很好;但如果你多次运行它而不重新启动程序,每次在第一次运行后将花费相同的时间,但进度条滞后,有时文件选择对话框不会消失,直到处理完成。这是下面的代码。我知道缩进是错误的,它不会正确复制。任何人都可以看到第一次运行后可能导致滞后的部分吗?

import sys,  time,  os
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(QtGui.QMainWindow):

    def updateProgress(self):
        self.progressBar.setValue(self.progressBar.value()+1)
        self.progressBar.repaint()

def processCollect(self):
    filename = None
    self.progressBar.setValue(0)

    #Get the filename from user
    try:
        filename = QtGui.QFileDialog.getOpenFileName(self,  "Open Collect",  sys.path[1] + "/",  "Text files (*.txt)")
    except IOError:
        filename == None

    if filename:
        #Find number of lines
        file = open(filename,  "r")
        linecount = 0
        for line in file:
            linecount = linecount+1
        file.close()
        print(linecount)

        #Set up progress bar
        self.progressBar.setMinimum(0)
        self.progressBar.setMaximum(linecount)
        self.progressBar.show()

        #Read file contents and update progress bar
        file = open(filename,  "r")
        for line in file:
            line = line.replace("\n", "")
            print(line)
            time.sleep(.05)
            self.updateProgress()
        file.close()

def setupUi(self, MainWindow):
    #Create the main window
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(800, 600)

    #Body of the main window
    self.centralwidget = QtGui.QWidget(MainWindow)

    #Add process collect button
    self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    self.centralwidget.buttonProcessCollect = QtGui.QPushButton(self.centralwidget)
    self.centralwidget.buttonProcessCollect.setGeometry(QtCore.QRect(310, 240, 120, 40))
    self.centralwidget.buttonProcessCollect.setObjectName(_fromUtf8("buttonProcessCollect"))

    #Add progress bar
    self.progressBar = QtGui.QProgressBar(self.centralwidget)
    self.progressBar.setGeometry(QtCore.QRect(165, 290, 430, 20))
    self.progressBar.setProperty("value", 0)
    self.progressBar.setObjectName("progressBar")
    self.progressBar.hide()

    #Add actions to body
    self.centralwidget.connect(self.centralwidget.buttonProcessCollect,  SIGNAL("clicked()"),  self.processCollect)

    #Add body to the menu
    MainWindow.setCentralWidget(self.centralwidget)

    #Add text
    self.retranslateUi(MainWindow)

    #Connect actions
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
    self.centralwidget.buttonProcessCollect.setText(QtGui.QApplication.translate("MainWindow", "Process Collect", None, QtGui.QApplication.UnicodeUTF8))

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:2)

这可以通过使用线程进行解析,以便不影响实际的用户界面。

您可以使用python线程模块或qt qthread模块。

Twio链接到该主题:

mailing list discussion

stackoverflow question asked earlier slightly diffrent

这是一个使用pyqt进行线程化的教程: threading pyqt4