在所附的代码中,当您单击开始时,它将创建一个QSpinBox
,并开始在QThread
中计数到20,但如果我在计数时再次单击开始,第一个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
现在还不清楚您在按stop或在停止后按start时希望发生什么,但下面的代码应该或多或少对您有用:
相关问题 更多 >
编程相关推荐