How to use clear() method β In Python
Syntax : label.clear()
Argument : It takes no argument.
Code :
# importing the required libraries from PyQt5.QtWidgets import * from PyQt5 import QtCore from PyQt5.QtGui import * import sys class Window(QMainWindow): def __init__( self ): super ().__init__() # set the title self .setWindowTitle( "Label" ) # setting the geometry of window self .setGeometry( 0 , 0 , 400 , 300 ) # creating a label widget self .label_1 = QLabel( "Label" , self ) # moving position self .label_1.move( 100 , 100 ) # setting up border self .label_1.setStyleSheet( "border: 1px solid black;" ) # creating a label widget self .label_2 = QLabel( "Hidden Label" , self ) # moving position self .label_2.move( 100 , 150 ) # setting up border self .label_2.setStyleSheet( "border: 1px solid black;" ) # clearing the data self .label_2.clear() # 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 ()) |
Output :
PyQt5 β How to clear the content of label | clear and setText method
In this article, we will see how we can easily clear/erase the content of the label of PyQt5 application. This can be done in two ways β
- Using
clear()
method, this will clear the content of the label. - Using
setText()
method with passing a blank string, this will update the content with blank string.