close() method in PyQt5

In this article, we will see how to use close() method which belongs to the QWidget class, this method is used to close the window in PyQt5 application. In other words by close() method the window get closed without manually closing it.

Syntax : self.close()

Argument : It takes no argument.

Code :

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required libraries
  
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore
from PyQt5 import QtGui
import sys
import time
  
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
        # set the title
        self.setWindowTitle("Close")
  
        # setting  the geometry of window
        self.setGeometry(0, 0, 400, 300)
  
        # creating a label widget
        self.label = QLabel("Icon is set", self)
  
        # moving position
        self.label.move(100, 100)
  
        # setting up border
        self.label.setStyleSheet("border: 1px solid black;")
  
        # show all the widgets
        self.show()
  
        # waiting for 2 second
        time.sleep(2)
  
        # closing the window
        self.close()
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

chevron_right


This will open the window and after 2 seconds it will automatically closes the window.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.