在下面的示例中,我想将信号从 A 发送到 Main,从 B 发送到 Main。接收到来自 A 的信号是可以的,但是 B 不工作。棘手的部分是 B 中的“计算”必须从 A 触发,而不是从 Main 触发。将不胜感激任何帮助。
主要的:
import sys
from PyQt5.QtCore import pyqtSlot, QObject
from PyQt5.QtWidgets import QApplication
from A import A
from B import B
class Main(QObject):
def __init__(self):
super(Main, self).__init__()
self.a = A()
self.b = B()
self.a.msg.connect(self.show_message)
self.b.msg.connect(self.show_message)
@pyqtSlot(str)
def show_message(self, text):
print(text)
def start(self):
self.a.dojob()
if __name__ == '__main__':
app = QApplication(sys.argv)
m = Main()
m.start()
sys.exit(app.exec_())
A类:
from PyQt5.QtCore import pyqtSignal, QObject
from B import B
class A(QObject):
msg = pyqtSignal(str)
def __init__(self):
super(A, self).__init__()
self.b = B()
def send_message(self):
self.msg.emit("from A")
def dojob(self):
self.send_message()
self.b.dojobB()
B类:
from PyQt5.QtCore import pyqtSignal, QObject
class B(QObject):
msg = pyqtSignal(str)
def __init__(self):
super(B, self).__init__()
def send_message(self):
self.msg.emit("from B")
def dojobB(self):
self.send_message()