I am trying to make a Window with multiple screens so for that I tried to use the Stacked Widget
container of PyQT5.
To try it out, I made a simple window that in the designer and then extracted its code.
But, when I run that code, I get an error 'QMainWindow' object has no attribute 'addWidget'
the code looks like this:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'stack.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_StackedWidget(object):
def setupUi(self, StackedWidget):
StackedWidget.setObjectName("StackedWidget")
StackedWidget.resize(400, 300)
self.page = QtWidgets.QWidget()
self.page.setObjectName("page")
self.label = QtWidgets.QLabel(self.page)
self.label.setGeometry(QtCore.QRect(130, 80, 111, 51))
self.label.setObjectName("label")
StackedWidget.addDockWidget(self.page)
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.label_2 = QtWidgets.QLabel(self.page_2)
self.label_2.setGeometry(QtCore.QRect(90, 160, 161, 91))
self.label_2.setObjectName("label_2")
StackedWidget.addDockWidget(self.page_2)
StackedWidget.
self.retranslateUi(StackedWidget)
StackedWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(StackedWidget)
def retranslateUi(self, StackedWidget):
_translate = QtCore.QCoreApplication.translate
StackedWidget.setWindowTitle(_translate("StackedWidget", "StackedWidget"))
self.label.setText(_translate("StackedWidget", "first page"))
self.label_2.setText(_translate("StackedWidget", "2nd page"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_StackedWidget()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
As the warning in the header of all pyuic generated files says, you should not edit them unless you really know what you're doing. And if you know what you're doing, you will not edit them.
That's a polite way of saying: you must never edit those files.
You cannot try to use a QMainWindow with a file generated for a QStackedWidget, as they are completely different widgets, with completely different purposes and functions.
Since you're probably going to use a QMainWindow anyway, the best solution is to create a QMainWindow in Designer, and add a QStackedWidget to it. Just ensure that you correctly set a layout: if the stacked widget is going to be the "main widget", a simple vertical/horizontal boxed layout set as main layout is sufficient: right click anywhere outside the stacked widget and select a proper layout from the "Lay out" submenu.
As an alternative, you can use the stacked widget as central widget directly from code, if you selected that widget from the new form dialog of Designer, and if you also properly follow the guidelines about using Designer, for example:
from PyQt5 import QtWidgets
from ui_stackedWidget import Ui_StackedWidget
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.stackedWidget = QtWidgets.QStackedWidget()
self.ui = Ui_StackedWidget()
self.ui.setupUi(self.stackedWidget)
self.setCentralWidget(self.stackedWidget)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())