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_())