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_())
效果图如下所示: