Close a window in PyQt5 in Python

Hey folks, This tutorial will help you to close a window in PyQt5 in Python. This can be achieved by using a simple method of the QWidget class. We will use that method and learn how to achieve our goal in a few ways. So for this, we are going to use some libraries of Python namely: PyQt5 and sys.

Using:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

How to close a window in PyQt5

To close a window in PyQt5 we use the .close() method on that window. We can simply achieve this by calling the .close() method whenever we want to close the window or by some following ways:

  1. Calling the .close() method whenever needed.
  2. Binding to a PushButton to trigger the .close() method.
  3. Creating a QAction and adding it to the window to trigger the .close() method.

 

Calling the .close() method whenever needed

Create your PyQt5 application and just call the .close() method whenever that window needs closing. For example:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Close Window")
        self.l1=QLabel("Let's Close this Window")
        self.l1.setAlignment(Qt.AlignCenter)
        self.setCentralWidget(self.l1)
        self.close()    #Closes the window at this point
app=QApplication(sys.argv)
window=MainWindow()
window.show()
app.exec_()

The above program creates a simple PyQt5 application with a QLabel as its central widget and then finally, closes it. So the window will be created, the widget will be added but it won’t be displayed as it was closed as soon as it was created.

NOTE: The above-mentioned program is just a demonstration on how to use .close() method when needed to close a window. It’s not a full-flexed application. So nothing will be displayed as mentioned earlier.

 

Binding to a PushButton to trigger the .close() method

How to close a window in PyQt5

In this way of closing the window, we create a QPushButton() and then bind a function(to close the window) to it which is triggered when that button is clicked. For example:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Close Window")
        self.b1=QPushButton("Let's Close this Window")
        self.b1.clicked.connect(lambda:self.close()) #function binded to the button self.b1
        self.setCentralWidget(self.b1)
app=QApplication(sys.argv)
window=MainWindow()
window.show()
app.exec_()

The above program creates a simple PyQt5 application with a QPushButton() as its central widget and then binds a function to trigger on click. The function is a simple lambda function that calls the self.close() method.It’s bound to QPushButton() using the .clicked.connect() method.So when the button is clicked the window closes.

 

Creating a QAction and adding it to the window to trigger the .close() method

How to close a window in PyQt5

In this way of closing the window, we create a QAction() and then add that action to the window we want to close using the .addAction() method. For example:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Close Window")
        self.l1=QLabel("Let's Close this Window")
        self.l1.setAlignment(Qt.AlignCenter)
        self.setCentralWidget(self.l1)
        self.exit=QAction("Exit Application",shortcut=QKeySequence("Ctrl+q"),triggered=lambda:self.exit_app)
        self.addAction(self.exit)
    def exit_app(self):
        print("Shortcut pressed") #verification of shortcut press
        self.close()
app=QApplication(sys.argv)
window=MainWindow()
window.show()
app.exec_()

The above program creates a simple PyQt5 application with a QLabel as its central widget. Then a QAction() is created with some parameters like the name of the action, shortcut and triggered. Where shortcut is the key or key sequence that is needed to be pressed to trigger that action. Also, the triggered parameter is where the function to call on that action being triggered is specified. Then that action is added to the window which needs to be closed using the .addAction() So when the shortcut specified(i.e. Ctrl+q ) is pressed, as expected, the window closes.

Also read:

Digital stopwatch GUI Application in Python – PyQt5

Leave a Reply

Your email address will not be published. Required fields are marked *