'Dialog box is not closing either on pressing accept or reject buttons in pyQt5

I this code sample of dialog box there are three buttons, browse , ok and cancel .I want to close the dialog box on pressing ok or close buttons, i.e. either accepted or rejected dialog box should be closed. The signal and slot is working as I have tested it with print but self.accept() and self.close() is not working, what is the error I don't understand ...!

######################################################################################

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
            
        Dialog.setObjectName("Dialog")
        Dialog.resize(435, 112)
        self.gridLayout_2 = QtWidgets.QGridLayout(Dialog)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(5)
        sizePolicy.setVerticalStretch(5)
        sizePolicy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth())
        self.lineEdit.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(False)
        font.setWeight(50)
        self.lineEdit.setFont(font)
        self.lineEdit.setStyleSheet("background-color: rgb(205, 217, 214);\n"
"color: rgb(116, 140, 135);")
        self.lineEdit.setObjectName("lineEdit")
        self.gridLayout_2.addWidget(self.lineEdit, 1, 1, 1, 1)
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout_2.addItem(spacerItem, 0, 0, 1, 1)
        self.LoadButtonBox = QtWidgets.QDialogButtonBox(Dialog)
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.LoadButtonBox.setFont(font)
        self.LoadButtonBox.setStyleSheet("background-color: rgb(205, 217, 214);\n"
"color: rgb(116, 140, 135);")
        self.LoadButtonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.LoadButtonBox.setObjectName("LoadButtonBox")
        self.gridLayout_2.addWidget(self.LoadButtonBox, 3, 0, 1, 2)
        self.Browse = QtWidgets.QPushButton(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.Browse.sizePolicy().hasHeightForWidth())
        self.Browse.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.Browse.setFont(font)
        self.Browse.setStyleSheet("background-color: rgb(205, 217, 214);\n"
"color: rgb(116, 140, 135);")
        self.Browse.setObjectName("Browse")
        self.gridLayout_2.addWidget(self.Browse, 1, 0, 1, 1)
        spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout_2.addItem(spacerItem1, 4, 0, 1, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.Browse.setText(_translate("Dialog", "BROWSE"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source