from PyQt5.QtWidgets import * app = QApplication([]) dialog = QDialog() buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) buttonBox.setEnabled(False) dialogLayout = QVBoxLayout() dialogLayout.addWidget(buttonBox) dialog.setLayout(dialogLayout) dialog.exec_()
from PyQt5.QtWidgets import * class MainWindow(QMainWindow): def __init__(self): super().__init__() buttonBox = QDialogButtonBox() buttonBox.setStandardButtons(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) action = QAction("Enable", self) action.triggered.connect(lambda: buttonBox.setEnabled(True)) toolbar = QToolBar() toolbar.addAction(action) self.addToolBar(toolbar) self.setCentralWidget(buttonBox) app = QApplication([]) window = MainWindow() window.show() app.exec_()This example demonstrates how the setEnabled method can be used with an QAction object to enable/disable the QDialogButtonBox buttons. When the "Enable" action is triggered, the setEnabled method is called on the buttonBox and given a value of True, enabling the buttons. Package library: PyQt5.QtWidgets.