运行 多个 qthread 在 Python 中并发
Run multiple qthreads concurrently in Python
在附加代码中,当您单击 开始 时,它会创建一个 QSpinBox
并在 QThread
中开始计数到 20,但是如果我单击 [=18] =]在计数时再次开始,第一个QSpinBox
停止并且一个新的焦点,并且两个计数器运行都在其中,但我需要所有旋转到运行同时分别:
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_())
我做了一些关键更改:
- 在覆盖同一个线程对象时保留了一个单独线程的列表,
- 修复了停止线程时的递归错误;使用
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_())
在附加代码中,当您单击 开始 时,它会创建一个 QSpinBox
并在 QThread
中开始计数到 20,但是如果我单击 [=18] =]在计数时再次开始,第一个QSpinBox
停止并且一个新的焦点,并且两个计数器运行都在其中,但我需要所有旋转到运行同时分别:
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_())
我做了一些关键更改:
- 在覆盖同一个线程对象时保留了一个单独线程的列表,
- 修复了停止线程时的递归错误;使用
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_())