python中多個QPushButton響應同一個事件
阿新 • • 發佈:2018-11-03
python中多個QPushButton響應同一個事件
注:在python2 的環境下執行
#! -*- coding:utf-8 -*- from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton import sys class WindowDemo(QWidget): def __init__(self): super(WindowDemo, self).__init__() btn1 = QPushButton(u'單一儲存', self) btn2 = QPushButton(u'連續儲存', self) btn3 = QPushButton(u'暫停連續儲存', self) btn1.clicked.connect(self.handle_camsave) btn2.clicked.connect(self.handle_camsave) btn3.clicked.connect(self.handle_camsave) hbox = QHBoxLayout() hbox.addWidget(btn1) hbox.addWidget(btn2) hbox.addWidget(btn3) hbox.addStretch(0) self.setLayout(hbox) self.setWindowTitle("addStretch 例子") def handle_camsave(self): sender = self.sender() clickevent = sender.text() if clickevent == u'單一儲存': print(u'單擊了第一個按鈕') elif clickevent == u'連續儲存': print(u'單擊了第二個按鈕') else: print(u'單擊了第三個按鈕') if __name__ == "__main__": app = QApplication(sys.argv) win = WindowDemo() win.show() sys.exit(app.exec_())
效果圖如下所示: