PyQt5--EventSender
1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 14, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: This example is show which sender was triggered. 8 创建了两个按钮。在buttonClicked()方法中通过调用sender()方法来判断当前按下的是哪个按钮 9 ''' 10 import sys 11 from PyQt5.QtWidgets import QApplication,QMainWindow,QPushButton 12 13 class New_test(QMainWindow): 14 def __init__(self): 15 super().__init__() 16 self.initUI() 17 18 def initUI(self): 19 btn1 = QPushButton('Button1',self) 20 btn1.move(30,50) 21 btn2 = QPushButton('Button2',self) 22 btn2.move(150,50) 23 24 btn1.clicked.connect(self.buttonClicked) 25 btn2.clicked.connect(self.buttonClicked) 26 27 self.statusBar() 28 29 self.setGeometry(300,300,290,150) 30 self.setWindowTitle('Event Sender') 31 self.show() 32 33 def buttonClicked(self): 34 sender = self.sender() 35 self.statusBar().showMessage(sender.text() + ' was pressed') # 通过调用sender()方法来判断信号源, 并将其名称显示在窗体的状态栏中 36 37 if __name__ == '__main__': 38 app = QApplication(sys.argv) 39 ex = New_test() 40 sys.exit(app.exec_())
按下 button1时在status bar的提示语:
按下 button2时在 status bar的提示语: