2

EDIT: Problem Solved

THE ANSWER IS NOT sys.exit()

The solution to the problem is using the ".accept" method in the ".connect" call. Since this is an instance of the QDialog class, you need to use ".accept".

 self.alerts_window_cancel_button.clicked.connect(CreateEditAlertsWindow.accept)

This is the working solution to have a button close just the QDialog window and not the entire program.


Original Question:

I have searched probably 30 questions and none of the solutions are working for me although some of the original posters have a similar situation. I hate asking questions on here, but here we go...

I'm creating a GUI application with PyQt5 and QtDesigner and I have a main window that opens a separate dialog window. I would like to close the dialog window when the "cancel" button is pressed.

From reading other answers on here, I've tried:

self.button.clicked.connect(self.close())
self.button.clicked.connect(self.exit())
self.button.clicked.connect(self.reject)

As well as linking the button to a function:

self.btn.clicked.connect(self.closeWindow)
def closeWindow(self):
    # self.exit()
    # ui.close()
    # app.close()

Here's my code. I tried to take out all of the irrelevant other stuff as this program is several hundred lines.

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Ui_CreateEditAlertsWindow(object):
    def setupUi(self, CreateEditAlertsWindow): 
        self.alerts_window_cancel_button = QtWidgets.QPushButton(CreateEditAlertsWindow)
        self.alerts_window_cancel_button.setObjectName ("alerts_window_cancel_button")
        self.horizontalLayout_2.addWidget(self.alerts_window_cancel_button)

        # a bunch of other stuff

    def retranslateUi(self, CreateEditAlertsWindow):
        _translate = QtCore.QCoreApplication.translate
        self.alerts_window_cancel_button.setText (_translate("CreateEditAlertsWindow", "Cancel"))

        self.alerts_window_cancel_button.clicked.connect(self.closeWindow)

        # a bunch more stuff

def closeWindow(self):
    self.exit()

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

Edit: The issue I'm running into is I get an "AttributeError: "Ui_CreateEditAlertsWindow" object has no attribute "exit"

JMerit
  • 41
  • 1
  • 5
  • Doesn't that terminate the whole program and not just the dialog window? – JMerit Dec 10 '18 at 16:22
  • eyllanesc, your solution did not work for me. sys.exit() closes the entire program. However, after speaking with some other folks it turns out that using the ".accept" function solves the problem I'm having. In short, since it's a QDialog class instance, I needed to use: `self.alerts_window_cancel_button.clicked.connect(CreateEditAlertsWindow.accept)` This made it to where when I click the "Cancel" button, it only closes the QDialog window and not the entire program. I wanted to make sure I explain this in the comment for future people to read and use. – JMerit Dec 10 '18 at 20:03
  • i got same problem on python. driving me crazy. why there isn't a simple way to do simple thing gosh – greendino Jun 24 '20 at 12:38

0 Answers0