Skip to content
Related Articles

Related Articles

close() method in PyQt5

Improve Article
Save Article
  • Last Updated : 26 Mar, 2020
Improve Article
Save Article

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 :

# 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):
        # set the title
        # 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
        # waiting for 2 second
        # closing the window
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!