self.worker_thread.start()运行的是什么函数
时间: 2023-06-13 13:06:26 浏览: 161
`self.worker_thread.start()`运行的是一个线程(Thread)中的`run()`函数。具体实现可能需要查看代码中`worker_thread`线程对象的初始化过程和`run()`函数的具体实现。一般来说,`worker_thread`线程对象的初始化会指定一个可调用对象(例如函数或方法)作为其`target`参数,当线程启动时,它会自动调用该可调用对象。在这个可调用对象中,一般会包含需要在线程中执行的具体业务逻辑,这些代码可以在`run()`函数中实现。
相关问题
# # 多线程实例3 method = [pieState] # 字典 self.thread_s1 = {} self.worker_s1 = {} for i in range(len(method)): self.thread_s1[i] = 'thread' + str(i) self.worker_s1[i] = 'worker' + str(i) for j in range(len(method)): self.thread_s1[j] = QThread() self.worker_s1[j] = Worker() self.worker_s1[j].moveToThread(self.thread_s1[j]) self.thread_s1[j].started.connect(self.worker_s1[j].run) self.worker_s1[j].finished.connect(self.thread_s1[j].quit) self.worker_s1[j].finished.connect(self.worker_s1[j].deleteLater) self.thread_s1[j].finished.connect(self.thread_s1[j].deleteLater) self.worker_s1[j].finished.connect(method[j]) # Start the thread self.thread_s1[j].start() #调用函数 # pieState() except Exception as alarm: message = QMessageBox.warning(stack1,'警告','数据库没有数据',QMessageBox.Ok)报警: return affine_transform(points, mtx) RuntimeError: In affine_transform: Invalid vertices array. 为什么没有触发try except机制
根据您提供的代码片段,我看不出来为什么try except机制没有被触发。但是,我注意到您在except语句块中使用了QMessageBox来弹出警告消息框。请确保在运行该代码的环境中,QMessageBox可以正常使用。另外,您可以在except语句块中添加一些print语句来输出异常信息,以便更好地调试问题。
self.main_ui.pushButton_2.clicked.connect(self.thing1) 没反应是为什么 class Worker(QtCore.QThread): sinOut = pyqtSignal(str) def __init__(self, parent=None): super(Worker, self).__init__(parent) # 设置工作状态与初始num数值 self.working = True self.num = 0 #def __del__(self): # 线程状态改变与线程终止 #self.working = False #self.wait() def stop(self): #线程状态改变与线程终止 self.working = False self.wait() def run(self): self.working = True while self.working == True: #file_str = 'File index{0}'.format(self.num) self.num += 1 # 发射信号 #self.sinOut.emit(file_str) self.sinOut.emit('1') # 线程休眠2秒 self.msleep(5) class parentWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.main_ui = JQ.Ui_MainWindow() self.main_ui.setupUi(self) self.thread1 = Worker() self.main_ui.pushButton_2.clicked.connect(self.thing1) self.thread1.sinOut.connect(self.printt) def thing1(self): #self.main_ui.pushButton.setEnabled(False) print('9999999999') self.thread1.start() print('123') #self.sleep(2) def printt(self): print('7777') def ok(): print('ok') # def hourstest(): # thread1 = Worker() # thread1.start() # com = JQ.Ui_MainWindow().comboBox_2.currentText() # ser = serial.Serial('com3', 1200, timeout=1) # data = bytes.fromhex( # '68 01 00 20 00 00 00 00 00 34 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 F4 16 ') # ser.write(data) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) widget = QtWidgets.QMainWindow() widget = parentWindow() ui = JQ.Ui_MainWindow() # 这是原py中的类,因人而异哦 ui.setupUi(widget) ui.discoverSerial() #串口发现 #ui.pushButton_2.clicked.connect(widget.thing1) widget.show() sys.exit(app.exec_())
首先,你的连接语句应该是正确的。但是,你的槽函数`thing1`中启动了一个线程并且没有等待该线程完成。因此,当你再次点击`pushButton_2`时,会创建一个新的线程,导致出现问题。
你需要在`thing1`函数中添加`self.thread1.wait()`语句来等待线程完成。这样,当你再次点击`pushButton_2`时,它会等待线程完成后再启动一个新的线程。
另外,你在`ok`函数中定义了一个serial对象,但是没有使用它。如果你想使用该对象,你需要在该函数中添加相关代码。
以下是修改后的代码:
```
class parentWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.main_ui = JQ.Ui_MainWindow()
self.main_ui.setupUi(self)
self.thread1 = Worker()
self.thread1.sinOut.connect(self.printt)
self.main_ui.pushButton_2.clicked.connect(self.thing1)
def thing1(self):
print('9999999999')
self.thread1.start()
self.thread1.wait() # 等待线程完成
print('123')
def printt(self):
print('7777')
def ok():
print('ok')
com = JQ.Ui_MainWindow().comboBox_2.currentText()
ser = serial.Serial('com3', 1200, timeout=1)
data = bytes.fromhex('68 01 00 20 00 00 00 00 00 34 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 F4 16 ')
ser.write(data)
```
阅读全文