下面是一个示例代码,展示了如何在保存记录之前弹出消息提示。
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QPushButton, QMessageBox
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("保存记录之前弹出消息提示")
layout = QVBoxLayout()
label = QLabel("点击按钮保存记录")
layout.addWidget(label)
button = QPushButton("保存记录")
button.clicked.connect(self.showConfirmationDialog)
layout.addWidget(button)
self.setLayout(layout)
def showConfirmationDialog(self):
confirmation = QMessageBox.question(self, "确认保存", "是否保存记录?", QMessageBox.Yes | QMessageBox.No)
if confirmation == QMessageBox.Yes:
self.saveRecord()
def saveRecord(self):
# 在这里添加保存记录的代码
print("保存记录")
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
在这个示例中,我们创建了一个QMainWindow
类的子类MainWindow
。在initUI
方法中,我们创建了一个标签label
和一个按钮button
。按钮被点击时,它将调用showConfirmationDialog
方法。
showConfirmationDialog
方法创建了一个QMessageBox
对话框,询问用户是否要保存记录。如果用户点击了“是”按钮,它将调用saveRecord
方法来保存记录。
在saveRecord
方法中,你可以添加保存记录的实际代码。
请注意,这只是一个示例,你需要根据你的实际需求进行调整和修改。