[Python] I want to display three identical QLabel widgets in a QGridLayout

Discussão em 'Python' iniciado por Stack, Abril 6, 2025.

  1. Stack

    Stack Membro Participativo

    I want to add the same Widget (a QLabel) to a layout (QGridLayout) multiple times. I only get the widget shown once.

    This is my code, I would expect to see the label 3 times, I only see it once

    import sys
    from PyQt6.QtWidgets import QApplication, QMainWindow, QGridLayout, QWidget, QLabel

    class MainWindow(QMainWindow):
    def __init__(self):
    super().__init__()

    self.setWindowTitle("My App")

    layout = QGridLayout()

    label = QLabel("Test")
    label1 = QLabel("Test1")
    label2 = QLabel("Test2")

    layout.addWidget(label, 0, 0)
    layout.addWidget(label, 0, 1)
    layout.addWidget(label, 0, 2)

    widget = QWidget()
    widget.setLayout(layout)
    self.setCentralWidget(widget)


    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()

    app.exec()


    If I bring label1 and label2 into play as in

    layout.addWidget(label, 0, 0)
    layout.addWidget(label1, 0, 1)
    layout.addWidget(label2, 0, 2)


    then I see all 3 (different) labels as I would expect.

    So is only being able to use a widget in a layout once by design? If yes then why? Or am I missing something?

    Continue reading...

Compartilhe esta Página