0

time error when I'm trying to close the app. Here's the code. I'm using pyqt5 designer.

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QDialog, QApplication, QMessageBox, QGridLayout
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
def setupUi(self, Dialog):
    #Form Dialog
    grid = QGridLayout()
    Dialog.setLayout(grid)
    Dialog.setObjectName("LoginForm")
    Dialog.resize(640, 480)

    #Button Exit
    self.btnEsc = QtWidgets.QPushButton(Dialog)

I'm having trouble on this part of the code "(self.closeEvent)"

    self.btnEsc.clicked.connect(self.closeEvent)

    self.btnEsc.setGeometry(QtCore.QRect(380, 310, 75, 61))
    self.btnEsc.setObjectName("btnEsc")

    #Form translate
    self.retranslateUi(Dialog)
    QtCore.QMetaObject.connectSlotsByName(Dialog)

when I close this part(running), it pops up a message "Python has stopped working"

def closeEvent(self, event):
    reply = QMessageBox.question(self, 'Message',
                                        "Are you sure to quit?",
                                        QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

    if reply == QMessageBox.Yes:
        event.accept()
    else:
        event.ignore()

I just want a customized for exit button, please help....

Music Lover
  • 33
  • 1
  • 5
  • change `self.btnEsc.clicked.connect(self.closeEvent)` to `self.btnEsc.clicked.connect(self.close)` – eyllanesc May 04 '18 at 05:59
  • Please fix you indentation. Please also include the part where you init your application. – MrLeeh May 04 '18 at 06:00
  • events should not be called directly, you should call close, and that method will call closeEvent correctly. – eyllanesc May 04 '18 at 06:00
  • @eyllanesc already did that part and I'm having an error if I change it to (self.close) Traceback (most recent call last): File "C:\Python36\Lib\site-packages\pyqt5-tools\UITestCodes\dialog.py", line 120, in ui.setupUi(main) File "C:\Python36\Lib\site-packages\pyqt5-tools\UITestCodes\dialog.py", line 90, in setupUi self.btnEsc.clicked.connect(self.close) AttributeError: 'Ui_Dialog' object has no attribute 'close' – Music Lover May 04 '18 at 06:25
  • @MusicLover read http://pyqt.sourceforge.net/Docs/PyQt5/designer.html#using-the-generated-code – eyllanesc May 04 '18 at 06:26

0 Answers0