clear() method in PyQt5

While using PyQt5, there occur a need of removing or clearing labels in order to do so clear() method is used. This method belongs to the QLabel class, and can be used on only label widgets.

Syntax : label.clear()
Argument : It takes no argument.
Action Performed : It clears(remove) the label from main window.

Below is the implementation of this method.

Code :

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required libraries
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 
import sys
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
        # set the title
        self.setWindowTitle("Move")
  
        # setting  the geometry of window
        self.setGeometry(0, 0, 500, 300)
  
        # creating a label widget
        self.label1 = QLabel('Label 1', self)
  
        # moving the widget
        # move(left, top)
        self.label1.move(100, 100)
  
        # creating a label widget
        self.label2 = QLabel('Label 2 ', self)
  
        # moving the widget
        # move(left, top)
        self.label2.move(100, 120)
  
        # clearing label 2
        self.label2.clear()
  
        # creating a label widget
        self.label3 = QLabel('Label 3', self)
  
        # moving the widget
        # move(left, top)
        self.label3.move(100, 140)
  
  
  
        # show all the widgets
        self.show()
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

chevron_right


Output :
clear-method-pyqt
As on the above image we can see Label2 will not be seen because of clear() method.

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.