I am writing an application in Qt using python that will process a txt file. The user selects a file from their hard drive and the program will open and process it. The main problem I am having is that the 1st time you run it, it is fine; but if you run it multiple times without restarting the program, each time after the 1st will take the same exact amount of time, but the progress bar lags and sometimes the file select dialog does not go away until the processing is complete. Here is the code below. I know the indentation is wrong, it would not copy properly. Can anyone see a part that may cause the lag after the 1st run?
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(_fromU开发者_如何学Ctf8("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_())
This is resolvable by using threads so that the actual user interface isn't affected.
You could either use the python thread module or the qt qthread module.
Twio links to that topic:
mailing list discussion
stackoverflow question asked earlier slightly diffrent
Here a tutorial to threading with pyqt: threading pyqt4
精彩评论