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

标签 python python-2.7 pyqt pyqt4

我的应用程序中有一个进度条,该进度条应该随着文件的读取而进度,但它根本不会根据我的设置条件进行更新。看来我需要为负责更新进度条的变量提供一个显式值,而不是另一个变量。请查看下面的代码,特别是我的 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()

关于python - pyqt 中的进度条未正确更新以读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41712612/

相关文章:

python - ProtocolError IncompleteRead 使用请求

python - 具有任意类型参数的 PyQt 信号/新型信号的 PyQt_PyObject 等价物

Python:从字典运行代码

Python请求循环,如果状态码不是200重试

python - 我的 Django 模型的权限

python - 如何从 unicode 字节表示中检索我的 unicode

python - 为什么 python glob 无法检测到我的拇指驱动器(我该怎么办?)

python - PyQt 对话框在退出时关闭整个应用程序

python - PyQtGraph 的 slider 小部件

python - 为什么 Python 的 nonlocal 关键字不像全局作用域那样?