[PyQt] Close program using Push Button with the help of input from a Message Box - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [PyQt] Close program using Push Button with the help of input from a Message Box (/thread-13651.html) |
Close program using Push Button with the help of input from a Message Box - bhargavbn - Oct-25-2018 Hello, I have created a basic application with a push button. The objective is for a pop-up message box to show up when I click on the push button. Based on the input provided in the push button , the program has to close. import sys from PyQt5.QtWidgets import QApplication,QWidget,QToolTip,QPushButton,QMessageBox from PyQt5.QtGui import QFont class Window(QWidget): def __init__(self): super().__init__() self.initUI() def closeevent(self): reply=QMessageBox.question(self,'Message','Are you sure you want to quit?',QMessageBox.Yes|QMessageBox.No,QMessageBox.No ) if reply==QMessageBox.Yes: event.accept() else: event.ignore() / def initUI(self): QToolTip.setFont(QFont('Arial',10,3)) self.setToolTip('This is <i>Window </i> Tooltip' ) btn1=QPushButton('Click here to close the pgm',self) btn1.setToolTip('This is <i>Button </i> Tooltip') btn1.resize(btn1.sizeHint()) btn1.clicked.connect(self.closeevent()) self.setGeometry(100,100,300,300) self.show() if __name__=="__main__": app=QApplication(sys.argv) obj1=Window() sys.exit(app.exec_()) Have created a function called closeevent(). On click of the button , this function has to be called. However when I call self.closeevent() like any other function, The window shows only the message box and not the complete window. Why is it so? RE: Close program using Push Button with the help of input from a Message Box - Alfalfa - Oct-25-2018 Your code output this error: It seems that you tried to override the widget closeEvent by defining a function called closeevent, but it does not work as functions in python are case sensitive. Also, as it is explained in the error above, to do so you would have to declare closeEvent with two arguments, like so:def closeEvent(self, event):But in your case, you don't need to do this at all: #!/usr/bin/python3 import sys from PyQt5.QtWidgets import QApplication,QWidget,QToolTip,QPushButton,QMessageBox from PyQt5.QtGui import QFont class Window(QWidget): def __init__(self): super().__init__() self.initUI() def prompt(self): reply=QMessageBox.question(self,'Message','Are you sure you want to quit?',QMessageBox.Yes|QMessageBox.No,QMessageBox.No ) if reply==QMessageBox.Yes: self.close() def initUI(self): QToolTip.setFont(QFont('Arial',10,3)) self.setToolTip('This is <i>Window </i> Tooltip' ) btn1=QPushButton('Click here to close the pgm',self) btn1.setToolTip('This is <i>Button </i> Tooltip') btn1.resize(btn1.sizeHint()) btn1.clicked.connect(self.prompt) self.setGeometry(100,100,300,300) self.show() if __name__=="__main__": app=QApplication(sys.argv) obj1=Window() sys.exit(app.exec_()) RE: Close program using Push Button with the help of input from a Message Box - bhargavbn - Oct-30-2018 Ok Thanks |