from PySide2.QtWidgets import QWidget, QLabel, QVBoxLayout, QApplication import sys class MyWidget(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): label = QLabel('This is a label', self) label.move(50, 50) # hide the label widget label.hide() self.setGeometry(300, 300, 300, 200) self.setWindowTitle('QLabel Hide Example') self.show() app = QApplication(sys.argv) w = MyWidget() sys.exit(app.exec_())
from PySide2.QtWidgets import QWidget, QLabel, QVBoxLayout, QHBoxLayout, QPushButton, QApplication import sys class MyWidget(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): label = QLabel('This is a label', self) label.move(50, 50) hide_btn = QPushButton('Hide Label', self) hide_btn.clicked.connect(label.hide) vbox = QVBoxLayout() vbox.addWidget(label) vbox.addWidget(hide_btn) hbox = QHBoxLayout() hbox.addLayout(vbox) self.setLayout(hbox) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('QLabel Hide Example') self.show() app = QApplication(sys.argv) w = MyWidget() sys.exit(app.exec_())In this example, a QLabel widget and a QPushButton widget are created. The QPushButton widget is used to hide the QLabel widget when it is clicked. The QLabel widget is added to a QVBoxLayout and the QPushButton widget is added to the same QVBoxLayout. Finally, the QVBoxLayout is added to a QHBoxLayout and set as the main layout.