gpt4 book ai didi

python - PyQt QThreads 通信

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:10 26 4
gpt4 key购买 nike

PyQt 应用程序有一个运行 SenderWorkerQThread,它每秒连续发出一个温度 信号。运行 ReceiverWorker 的第二个 QThread 用于接收发出的信号并将其打印到屏幕上。

问题:但是ReceiverWorker没有响应发出的信号。也许使用以下代码行将它们连接在一起不起作用?

self.receiverWorker.connectSlots(self.senderWorker)

这是完整的代码:

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



class Screen(QMainWindow):
def __init__(self):
super(Screen, self).__init__()
self.initUI()

def initUI(self):
self.lightsBtn = QPushButton('Turn On')
self.lightsBtn.setCheckable(True)
self.lightsBtn.setStyleSheet("QPushButton:checked {color: white; background-color: green;}")
self.lightsBtn.clicked.connect(self.startLightsThread)

self.setCentralWidget(self.lightsBtn)

def startLightsThread(self):
print 'start lightsThread'
self.senderThread = QThread()
self.senderWorker = SenderWorker()
self.senderWorker.moveToThread(self.senderThread)
self.senderThread.started.connect(self.senderWorker.work)
self.senderThread.start()

self.receiverThread = QThread()
self.receiverWorker = ReceiverWorker()
self.receiverWorker.connectSlots(self.senderWorker)
self.receiverWorker.moveToThread(self.receiverThread)
self.receiverThread.start()



class SenderWorker(QObject):

temperatures = pyqtSignal(object)

def __init__(self):
QObject.__init__(self)
self._mutex = QMutex()
self._running = True

@pyqtSlot()
def work(self):
while self._running:
print 'Sender working'
self.temperatures.emit('123')
time.sleep(1)



class ReceiverWorker(QObject):

def __init__(self):
QObject.__init__(self)
self._mutex = QMutex()
self._running = True

def connectSlots(self, sender):
self.connect(sender, SIGNAL('temperatures'), self.work(temperatures))

def work(self, temperatures):
print 'Receiver working: ', temperatures



app = QApplication(sys.argv)
window = Screen()
window.show()
sys.exit(app.exec_())
<小时/>

更新#1

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



class Screen(QMainWindow):
def __init__(self):
super(Screen, self).__init__()
self.initUI()

def initUI(self):
self.lightsBtn = QPushButton('Turn On')
self.lightsBtn.setCheckable(True)
self.lightsBtn.setStyleSheet("QPushButton:checked {color: white; background-color: green;}")
self.lightsBtn.clicked.connect(self.startLightsThread)

self.setCentralWidget(self.lightsBtn)

def startLightsThread(self):
print 'start lightsThread'
self.senderThread = QThread()
self.senderWorker = SenderWorker()
self.senderWorker.moveToThread(self.senderThread)
self.senderThread.started.connect(self.senderWorker.work)
self.senderThread.start()

self.receiverThread = QThread()
self.receiverWorker = ReceiverWorker()
self.receiverWorker.connectSlots(self.senderWorker)
self.receiverWorker.moveToThread(self.receiverThread)
self.receiverThread.start()



class SenderWorker(QObject):

temperatures = pyqtSignal(object)

def __init__(self):
QObject.__init__(self)
self._mutex = QMutex()
self._running = True

@pyqtSlot()
def work(self):
while self._running:
print 'Sender working'
self.temperatures.emit('123')
time.sleep(1)



class ReceiverWorker(QObject):

def __init__(self):
QObject.__init__(self)
self._mutex = QMutex()
self._running = True

def connectSlots(self, sender):
sender.temperatures.connect(self.work)

def work(self, temperatures):
print 'Receiver working: ', temperatures



app = QApplication(sys.argv)
window = Screen()
window.show()
sys.exit(app.exec_())

最佳答案

问题出在连接上,如果你想使用旧的样式,你必须输入以下语法:

def connectSlots(self, sender):
self.connect(sender, SIGNAL('temperatures(PyQt_PyObject)'), self.work)

new style更简单({sender}.{signal}.connect({slot})):

def connectSlots(self, sender):
sender.temperatures.connect(self.work)

关于python - PyQt QThreads 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43598157/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com