If the user interacts with the application, for example pressing a button, and the user clicks then on the X button, the application keeps running, but the window closes. How can I fully terminate the application. It’s built using PyQt5.
Asked
Active
Viewed 3,546 times
2 Answers
2
Try it:
import sys
from PyQt5.QtWidgets import (QMainWindow, QLabel, QGridLayout, qApp,
QApplication, QWidget, QPushButton)
from PyQt5.QtCore import QSize, Qt
class HelloWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Hello world")
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
title = QLabel("Hello World from PyQt")
title.setAlignment(Qt.AlignCenter)
button = QPushButton("Quit")
button.clicked.connect(qApp.quit) # <---
gridLayout = QGridLayout(centralWidget)
gridLayout.addWidget(title, 0, 0)
gridLayout.addWidget(button, 1, 0)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = HelloWindow()
mainWin.show()
sys.exit( app.exec_() )

S. Nick
- 12,879
- 8
- 25
- 33
-
A quit button works fine, it’s just the window close button – yannh Dec 27 '18 at 12:52
-
`qApp` - A global pointer referring to the unique application object. It is equivalent to QCoreApplication::instance(), but cast as a QApplication pointer, so only valid when the unique application object is a QApplication. The `quit ()` method - tells the application to exit with return code 0 (success). Please specify what you need to do? – S. Nick Dec 27 '18 at 13:12
0
Here is a simple "Hello World" example, I copied from the Qt tutorials. It uses sys.exit(...)
to exit the application.
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtCore import QSize
class HelloWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(640, 480))
self.setWindowTitle("Hello world")
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QGridLayout(self)
centralWidget.setLayout(gridLayout)
title = QLabel("Hello World from PyQt", self)
title.setAlignment(QtCore.Qt.AlignCenter)
gridLayout.addWidget(title, 0, 0)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = HelloWindow()
mainWin.show()
sys.exit( app.exec_() )

Duck Dodgers
- 3,409
- 8
- 29
- 43
-
-
Then you have to show your code in the question. You can try this simple example on your pc to verify that the python script indeed ends. Without the code you are running, it is impossible to say what is going wrong. – Duck Dodgers Dec 27 '18 at 12:12