|
| 1 | +import sys |
| 2 | +from PyQt5.QtCore import Qt |
| 3 | +from PyQt5.QtWidgets import QPushButton, QApplication, QMessageBox, QWidget |
| 4 | + |
| 5 | + |
| 6 | +class Example(QWidget): |
| 7 | + def __init__(self): |
| 8 | + super().__init__() |
| 9 | + self.initUI() |
| 10 | + |
| 11 | + def initUI(self): |
| 12 | + btn1 = QPushButton("Button 1", self) |
| 13 | + btn1.resize(150, 40) |
| 14 | + btn1.move(400 / 2 - 150 / 2, 200 / 2 - 40) |
| 15 | + btn1.clicked.connect(self.buttonClicked) |
| 16 | + |
| 17 | + btn2 = QPushButton("Button 2", self) |
| 18 | + btn2.resize(150, 40) |
| 19 | + btn2.move(400 / 2 - 150 / 2, 200 / 2) |
| 20 | + btn2.clicked.connect(self.buttonClicked) |
| 21 | + |
| 22 | + self.resize(400, 200) |
| 23 | + self.setMaximumSize(400, 200) |
| 24 | + self.setWindowTitle('Event PyQT5') |
| 25 | + self.show() |
| 26 | + |
| 27 | + def buttonClicked(self, e): |
| 28 | + btn_txt = self.sender().text() |
| 29 | + QMessageBox.information(self, 'Events - Slot', 'click en: ' + btn_txt) |
| 30 | + |
| 31 | + def closeEvent(self, event): |
| 32 | + reply = QMessageBox.question(self, |
| 33 | + 'Events - Slot', |
| 34 | + "Realmente desea cerrar la aplicacion", |
| 35 | + QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) |
| 36 | + |
| 37 | + if reply == QMessageBox.Yes: |
| 38 | + event.accept() |
| 39 | + else: |
| 40 | + event.ignore() |
| 41 | + |
| 42 | + def keyPressEvent(self, event): |
| 43 | + if event.key() == Qt.Key_Escape: |
| 44 | + self.close() |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == '__main__': |
| 48 | + app = QApplication(sys.argv) |
| 49 | + win = Example() |
| 50 | + sys.exit(app.exec_()) |
| 51 | + |
| 52 | + |
0 commit comments