Open In App

PyQt5 – How to clear the content of label | clear and setText method

Improve
Improve
Like Article
Like
Save
Share
Report

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 –

  1. Using clear() method, this will clear the content of the label.
  2. Using setText() method with passing a blank string, this will update the content with blank string.

Using clear() method –

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 :
pyqt-clear-label

Using setText() method –

Syntax : label.setText(“”)

Argument : It takes string as argument, here string will be blank.

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;")
  
        # replacing content with blank
        self.label_2.setText("")
  
  
        # 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 :
pyqt-label-setText



Last Updated : 26 Mar, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads