2017-02-06 3 views
1

Ich versuche, 2 verschiedene Dateien gleichzeitig mit Pyqt und Threads zu lesen, aber nur ein Thread wird von den beiden ausgeführt. Mein Code hat 2 Thread-Klassen und jede ist verantwortlich für das Lesen ihrer zugewiesenen Dateien. Wie kann ich das erreichen?Wie kann ich 2 Threads in pyqt gleichzeitig ausführen?

Hier ist, was ich versucht habe:

import sys 
from PyQt4 import QtCore, QtGui 
import subprocess 
from time import sleep 

class Thread1(QtCore.QThread): 
    def __init__(self): 
     QtCore.QThread.__init__(self) 

    def file_len(self): 
     p = subprocess.Popen(['wc', '-l', 'file1.txt'], stdout=subprocess.PIPE, 
          stderr=subprocess.PIPE) 
     result, err = p.communicate() 
     if p.returncode != 0: 
      raise IOError(err) 
     return int(result.strip().split()[0]) #returns 600 lines 

    def run(self): 
     self.emit(QtCore.SIGNAL('updateProgressBar(int)'), 0) ## Reset progressbar value 
     file_in = "file1.txt" 
     loading = 0 
     x = float(100)/self.file_len() 
     with open(file_in) as f: 
      for line in f: 
       loading += x 
       print line 
       self.emit(QtCore.SIGNAL('updateProgressBar(int)'), loading) 
       sleep(0.15) 

class Thread2(QtCore.QThread): 
    def __init__(self): 
     QtCore.QThread.__init__(self) 

    def file_len(self): 
     p = subprocess.Popen(['wc', '-l', 'file2.txt'], stdout=subprocess.PIPE, 
          stderr=subprocess.PIPE) 
     result, err = p.communicate() 
     if p.returncode != 0: 
      raise IOError(err) 
     return int(result.strip().split()[0]) #returns 2500 lines 

    def run(self): 
     self.emit(QtCore.SIGNAL('updateProgressBar(int)'), 0) ## Reset progressbar value 
     file_in = "file2.txt" 
     loading = 0 
     x = float(100)/self.file_len() 
     with open(file_in) as f: 
      for line in f: 
       loading += x 
       print line 
       self.emit(QtCore.SIGNAL('updateProgressBar(int)'), loading) 
       sleep(0.001) 



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.progress2 = QtGui.QProgressBar(self) 

     verticalLayout = QtGui.QVBoxLayout(self) 
     verticalLayout.addWidget(self.buttonStart) 
     verticalLayout.addWidget(self.progress) 
     verticalLayout.addWidget(self.progress2) 
     self.progressView = Thread1() 
     self.progressView2 = Thread2() 
     self.connect(self.progressView, QtCore.SIGNAL("updateProgressBar(int)"), self.updateProgressBar) 
     self.connect(self.progressView2, QtCore.SIGNAL("updateProgressBar2(int)"), self.updateProgressBar2) 
     self.start() 

    def updateProgressBar(self, percent): 
     self.progress.setValue(percent) 

    def updateProgressBar2(self, percent): 
     self.progress2.setValue(percent) 


    def start(self): 
     self.progressView.start() 
     self.progressView2.start() 

if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    appview = AppView() 
    appview.show() 
    sys.exit(app.exec_()) 

Antwort

1

Vielleicht ist die Methode run von thread2 muss updateProgressBar2 nennen, nicht updateProgressBar?

+0

danke, du hast Recht. Tut mir leid, dass ich so eine dumme Frage gestellt habe. – answerSeeker

Verwandte Themen