from PyQt5.Qt import * import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("信號(hào)和槽") self.resize(600, 500) self.func_list() def func_list(self): self.func() self.on_btn_clicked() def func(self): btn = QPushButton('按鈕', self) btn.setObjectName('btn') btn.resize(80, 50) btn.move(100, 100) btn2 = QPushButton('按鈕', self) btn2.setObjectName('btn2') btn2.resize(80, 50) btn2.move(100, 200) # 表示把window對(duì)象中的子對(duì)象按照objectName鏈接到相關(guān)的槽函數(shù)上,必須放在所有子對(duì)象的最后 QMetaObject.connectSlotsByName(self) @pyqtSlot(bool) def on_btn_clicked(self): print('點(diǎn)擊按鈕') # 上面的代碼轉(zhuǎn)換 # self.btn.clicked[bool].connect(self.btn_clicked) # def btn_clicked(self): # print('點(diǎn)擊按鈕') if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
?
本文摘自 :https://www.cnblogs.com/