QWidget::setLayout: Attempting to set QLayout "" on Window "", which already has a layout

Heads up! You've already completed this tutorial.

I am a Beginner. I do not understand this error message. Who can explain it to me. :-)

Attempting to set QLayout "" on Window "", which already has a layout

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
    QApplication, QWidget, QMainWindow,
    QVBoxLayout, QHBoxLayout, QFormLayout, QGridLayout, QStackedLayout,
    QPushButton, QComboBox, QLineEdit, QCheckBox, QRadioButton,
class Window(QWidget):
    def __init__(self):

        layVer = QVBoxLayout()
        self.comBox = QComboBox()
        self.comBox.addItems(["One", "Two", "Three"])
        self.comBox.activated.connect(lambda x: self.onComBox(x))

        layHor = QHBoxLayout()
        self.cheBox1 = QCheckBox("A")


    def onCheBox(self, s):

    def onComBox(self, s):
        print(s, self.comBox.currentText())

if __name__ == "__main__":
    app = QApplication([])
    win = Window()


Firstly, delete the line self.setLayout(layVer)

then fix



to be



Martin Fitzpatrick

This error occurs because you're trying to set a layout on a QMainWindow. The QMainWindow is a special widget which has a built-in layout, to handle things link menus, toolbars and dock widgets.

For windows based on QMainWindow you can only set a central widget -- that is, the widget that appears in the center of the window. But if you remember from the PyQt5 layouts tutorial you can set a layout on an empty QWidget and add anything you want to it that way.

So the trick is: for controlling the layout of a mainwindow we first create a QWidget to act as a "container", use that widget as our central widget and apply the layout to it to add other widgets.

_Ali Sanawi Garrousi__

Merci now it works!

The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

QWidget::setLayout: Attempting to set QLayout "" on Window "", which already has a layout was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.