PyQT5之自定義訊號

星空28發表於2024-06-08
from PyQt5.QtCore import *


class MyTypeSignal(QObject):

    # 定義一個訊號
    sendmsg = pyqtSignal(object)

    # 傳送3個引數的訊號
    sendmsg1 = pyqtSignal(str, int, int)

    def run(self):
        self.sendmsg.emit("hello PyQt5")

    def run1(self):
        self.sendmsg1.emit("hello world", 6, 7)


class MySlot(QObject):
    def get(self, msg):
        print("資訊:" + msg)

    def get1(self, msg, a, b):
        print("資訊:" + msg, "今天是{}月{}日".format(a, b))


if __name__ == '__main__':
    send = MyTypeSignal()
    slot = MySlot()

    send.sendmsg.connect(slot.get)
    send.sendmsg1.connect(slot.get1)
    send.run()     # 傳送訊號到slot.get
    send.run1()     # 傳送訊號到slot.get1
    # send.sendmsg.disconncet(slot.get)   # 斷開連線

相關文章