Skip to main content
 首页 » 编程设计

python之pyqt 中的进度条未正确更新以读取文件

2024年10月17日8cyq1162

我在一个应用程序中有一个进度条,它应该随着文件的读取而前进,但它根本没有根据我的设置条件进行更新。看来我需要给负责更新进度条的变量一个明确的值而不是另一个变量。请查看下面的代码,特别是我的 loadfile 函数。

import sys 
from PyQt4 import QtCore, QtGui 
import subprocess 
from time import sleep 
 
class AppView(QtGui.QDialog): 
 
    def __init__(self, parent=None): 
        super(AppView, self).__init__(parent) 
        self.resize(400, 400) 
        self.buttonStart = QtGui.QPushButton(self) 
        self.buttonStart.setText("Start") 
        self.buttonStart.clicked.connect(self.start) 
 
        self.progress = QtGui.QProgressBar(self) 
        self.progress.setGeometry(200, 80, 250, 20) 
        verticalLayout = QtGui.QVBoxLayout(self) 
        verticalLayout.addWidget(self.buttonStart) 
        verticalLayout.addWidget(self.progress) 
 
    def line_count(self): 
        p = subprocess.Popen(['wc', '-l', 'xfile'], stdout=subprocess.PIPE, 
                             stderr=subprocess.PIPE) 
        result, err = p.communicate() 
        if p.returncode != 0: 
            raise IOError(err) 
        return int(result.strip().split()[0]) #returns 407 lines 
 
    def start(self): 
        self.loadfile() 
 
    def loadfile(self): 
        x = 100/self.line_count() 
        loading = 0 
 
        file_in = "xfile" 
        with open(file_in) as f: 
            for line in f: 
                #sleep(0.1) 
                print line 
                loading += x 
                #loading += 0.245700246 
                self.progress.setValue(loading) 
 
if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    appview = AppView() 
    appview.show() 
    sys.exit(app.exec_())  

但是,如果我像这样设置加载 loading += 0.245700246 它会起作用。我不明白为什么 loading += x 不做同样的事情,因为它也返回 0.245700246。

另一个问题是,当它工作并且进度条正在更新时,整个 UI 会被卡住。就像它正在使用所有 ui 线程,我还不知道如何解决这个问题。我无法关闭该应用程序或执行任何其他操作。

请您参考如下方法:

你不能在主线程中更新你的进度条,你必须创建另一个来做到这一点

class Progress(QtCore.QThread): 
    def __init__(self): 
        QtCore.QThread.__init__(self) 
 
    def run(self): 
        self.emit(QtCore.SIGNAL('__updateProgressBar(int)'), 0) ## Reset progressbar value 
        file_in = "xfile" 
        loading = 0 
        with open(file_in) as f: 
            fl_content = f.read().splitlines() 
            total_lines = len(fl_content) 
            for i, line in enumerate(fl_content): 
                print line 
                self.emit(QtCore.SIGNAL('__updateProgressBar(int)'), i*100/total_lines) 
                sleep(0.1) 
 
class AppView(QtGui.QDialog): 
 
    def __init__(self, parent=None): 
        super(AppView, self).__init__(parent) 
        self.resize(400, 400) 
        self.buttonStart = QtGui.QPushButton(self) 
        self.buttonStart.setText("Start") 
        self.buttonStart.clicked.connect(self.start) 
 
        self.progress = QtGui.QProgressBar(self) 
        self.progress.setGeometry(200, 80, 250, 20) 
        verticalLayout = QtGui.QVBoxLayout(self) 
        verticalLayout.addWidget(self.buttonStart) 
        verticalLayout.addWidget(self.progress) 
 
        self.progressView = Progress() 
        self.connect(self.progressView, QtCore.SIGNAL("__updateProgressBar(int)"), self.__updateProgressBar) 
 
    @QtCore.pyqtSlot(int) 
    def __updateProgressBar(self, percent): 
        self.progress.setValue(percent) 
 
    def start(self): 
        self.progressView.start()