PyQT5之訊號關閉視窗

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


class WinSignal(QWidget):

    # 定義一個訊號
    button_clicked_signal = pyqtSignal()

    def __init__(self):
        super().__init__()
        self.setWindowTitle("為視窗類新增訊號")
        self.resize(300, 100)

        btn = QPushButton("關閉視窗", self)
        btn.clicked.connect(self.btn_clicked)  # 1.點選觸發槽函式,2.槽函式觸發訊號,3.訊號關閉視窗

        self.button_clicked_signal.connect(self.btn_close)

    def btn_clicked(self):
        self.button_clicked_signal.emit()

    def btn_close(self):
        self.close()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    main_win = WinSignal()
    main_win.show()
    sys.exit(app.exec_())

相關文章