from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QApplication app = QApplication([]) dialog = QDialog() button_box = QDialogButtonBox(dialog) ok_button = button_box.addButton("OK", QDialogButtonBox.AcceptRole) cancel_button = button_box.addButton("Cancel", QDialogButtonBox.RejectRole) dialog.exec_()
from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QPushButton, QVBoxLayout, QApplication app = QApplication([]) dialog = QDialog() button_box = QDialogButtonBox(dialog) custom_button = QPushButton("Custom Button") button_box.addButton(custom_button, QDialogButtonBox.ActionRole) layout = QVBoxLayout(dialog) layout.addWidget(button_box) dialog.exec_()In this example, we create a dialog box and a button box. Instead of using the predefined AcceptRole and RejectRole roles, we create a QPushButton and add it to the button box using the ActionRole parameter. The dialog is then executed with the custom button added to the button box. Package library: PyQt5.