i am using python 2.7 with PyQT5, this is my button:
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(50, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.buttonBox.clicked.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
etc....
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QDialog()
ui = Ui_Dialog()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())
how can I execute a function after click OK??
Don't connect to buttonBox.clicked
, because that will be called for every button.
Your button-box connections should look like this:
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
To run a function/slot when the dialog is accepted (i.e. only when the OK button is clicked), do this:
self.accepted.connect(some_function)
If you want to pass a parameter, use a lambda
:
self.accepted.connect(lambda: some_function(param))
You buttonBox setup should look like
self.buttonBox.clicked.connect(Dialog.accept)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(Dialog.reject)
where self.accept
is a function defined into the class.
def accept(self):
If you need to pass some parameters to the function you need to store these parameters into some class variables instead passing them as params into the function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With