PyQt5 – 如何隐藏标签 | label.setHidden方法
在这篇文章中,我们将看到如何在PyQt5应用程序中隐藏标签。标签是一个图形控制元素,在表单中显示文本。它通常是一个静态控件,没有交互性。标签通常用于识别附近的文本框或另一个小部件。为了隐藏标签,我们使用setHidden()方法,这个方法允许用户设置该部件是可见还是隐藏,它属于QWidget类。
语法: label.setHidden(True)
参数: 它接受bool作为参数。
代码。
# 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("Normal 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;")
 
        # hiding the label
        self.label_2.setHidden(True)
 
 
        # 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())
输出 :

我们可以看到,标签2在输出窗口中被隐藏了。
极客教程