Python Forum
[PyQt] Close program using Push Button with the help of input from a Message Box
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Close program using Push Button with the help of input from a Message Box
#1
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?
Reply
#2
Your code output this error:


Error:
Traceback (most recent call last): File "/home/will/DATA/Downloads/pyScript.py", line 31, in <module> obj1=Window() File "/home/will/DATA/Downloads/pyScript.py", line 10, in __init__ self.initUI() File "/home/will/DATA/Downloads/pyScript.py", line 25, in initUI btn1.clicked.connect(self.closeevent()) File "/home/will/DATA/Downloads/pyScript.py", line 17, in closeevent event.ignore() NameError: name 'event' is not defined
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_())
Reply
#3
Ok Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - touchscreen, push the button like click the mouse John64 5 739 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,369 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? Osman_P 4 5,232 Nov-14-2020, 10:51 AM
Last Post: Osman_P
  [Tkinter] Open a python program from a button Pedroski55 3 4,881 Jul-20-2020, 11:09 PM
Last Post: Pedroski55
  [Tkinter] How to close all windows when exit program scratchmyhead 3 6,306 May-17-2020, 08:19 AM
Last Post: menator01
  [Tkinter] Creating a restart button for a Pythagorean Theorem Program pav1983 12 7,159 Apr-21-2020, 07:25 AM
Last Post: pav1983
  [PyQt] Push Button issue gvin47 8 4,261 Apr-18-2020, 05:39 PM
Last Post: deanhystad
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,947 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  Windows GUI with push buttons to launch python scripts drifterf 7 4,120 Jul-17-2019, 05:34 PM
Last Post: Yoriz
  Text after push button chano 13 5,230 Jul-05-2019, 03:10 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020