2015-04-12 71 views
0

在所附的代碼,當你點擊開始它創建一個QSpinBox,並開始在QThread數到20,但如果我再次點擊開始同時計數,第一QSpinBox停止,並新的需要關注的焦點,而這兩個計數器在運行它,但我需要的所有旋轉以分別同時運行:運行多個qthreads

import sys 
import time 
from PySide.QtGui import * 
from PySide.QtCore import * 

class frmMain(QDialog): 
    def __init__(self): 
     QDialog.__init__(self) 
     self.btStart = QPushButton('Start') 
     self.btStop = QPushButton('Stop') 
     self.counter = QSpinBox() 
     self.layout = QVBoxLayout() 
     self.layout.addWidget(self.btStart) 
     self.layout.addWidget(self.btStop) 
     self.layout.addWidget(self.counter) 
     self.setLayout(self.layout) 
     self.btStart.clicked.connect(self.start_thread) 
     self.btStop.clicked.connect(self.stop_thread) 
     self.boxes = [] 

    def stop_thread(self): 
     self.th.stop() 

    def loopfunction(self, x): 
     self.boxes[-1].setValue(x) 

    def start_thread(self): 
     self.th = thread(2) 
     self.th.loop.connect(self.loopfunction) 
     self.th.setTerminationEnabled(True) 
     self.boxes.append(QSpinBox()) 
     self.layout.addWidget(self.boxes[-1]) 
     self.th.start() 

class thread(QThread): 
    loop = Signal(object) 

    def __init__(self, x): 
     QThread.__init__(self) 
     self.x = x 

    def run(self): 
     for i in range(20): 
      self.x = i 
      self.loop.emit(self.x) 
      time.sleep(0.5) 

    def stop(self): 
     self.stop() 


app = QApplication(sys.argv) 
win = frmMain() 

win.show() 
sys.exit(app.exec_()) 
+0

所以,你希望這是一個每次啓動都會點擊一個新的旋轉框,每個人都保持它自己的時間?或者您是否只希望一個旋轉盒在重新按下開始按鈕時重置? – 101

+0

每次點擊新的旋轉框 –

+0

應該停止按鈕停止所有定時器嗎? – 101

回答

0

我做了一些重要變化:

  • 保留了一個單獨的線程列表,因爲您正在覆蓋同一個線程對象,
  • 修復了停止線程時的遞歸錯誤;使用terminate代替
  • 線程跟蹤其自己的索引,以便知道要更新哪個旋轉框。

這不是真的清楚你想,當你按下停止,或者按開始停藥後發生的事情,但是這個代碼應該更多的還是你少工作:

import sys 
import time 
from PySide.QtGui import * 
from PySide.QtCore import * 

class frmMain(QDialog): 
    def __init__(self): 
     QDialog.__init__(self) 
     self.btStart = QPushButton('Start') 
     self.btStop = QPushButton('Stop') 
     #self.counter = QSpinBox() 
     self.layout = QVBoxLayout() 
     self.layout.addWidget(self.btStart) 
     self.layout.addWidget(self.btStop) 
     #self.layout.addWidget(self.counter) 
     self.setLayout(self.layout) 
     self.btStart.clicked.connect(self.start_thread) 
     self.btStop.clicked.connect(self.stop_thread) 
     self.boxes = [] 
     self.threads = [] 

    def stop_thread(self): 
     for th in self.threads: 
      th.terminate() 

    def loopfunction(self, n, index): 
     self.boxes[index].setValue(n) 

    def start_thread(self): 
     index = len(self.threads) 
     th = thread(index) 
     th.loop.connect(self.loopfunction) 
     th.setTerminationEnabled(True) 
     th.start() 
     self.threads.append(th)   
     self.boxes.append(QSpinBox()) 
     self.layout.addWidget(self.boxes[index])   


class thread(QThread): 
    loop = Signal(int, int) 

    def __init__(self, index): 
     QThread.__init__(self) 
     self.index = index 

    def run(self): 
     for n in range(20): 
      self.loop.emit(n, self.index) 
      time.sleep(0.5) 


app = QApplication(sys.argv) 
win = frmMain() 

win.show() 
sys.exit(app.exec_()) 
+0

謝謝,它工作:) –