【发布时间】:2017-06-02 10:18:49
【问题描述】:
我在一个应用程序中有一个进度条,它应该在文件被读取时进行,但它根本没有按照我设定的条件进行更新。看来我需要给负责更新进度条的变量一个显式值而不是另一个变量。请看下面的代码,特别是我的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 线程,我还不知道如何解决这个问题。我无法关闭应用程序或执行任何其他操作。
【问题讨论】:
标签: python python-2.7 pyqt pyqt4