PyQt处理导致GUI延迟

2024-05-13 13:49:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Qt编写一个应用程序,它将处理一个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_())

Tags: importselfaddsyslinefilenamefileqtgui