Долго игрался с Layout'ами и sizePolicy - ничего не выходит. При использовании resizeEvent нужно для каждого виджета прописывать setGeometry, что очень долго и неудобно.
Подскажите пожалуйста оптимальное решение.
main_UI.py
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(400, 400) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.stackedWidget = QtWidgets.QStackedWidget(self.centralwidget) self.stackedWidget.setGeometry(QtCore.QRect(0, 0, 400, 400)) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.stackedWidget.sizePolicy().hasHeightForWidth()) self.stackedWidget.setSizePolicy(sizePolicy) self.stackedWidget.setObjectName("stackedWidget") self.homePage = QtWidgets.QWidget() self.homePage.setObjectName("homePage") self.widget = QtWidgets.QWidget(self.homePage) self.widget.setGeometry(QtCore.QRect(90, 120, 231, 101)) self.widget.setObjectName("widget") self.homePage_HLayout = QtWidgets.QHBoxLayout(self.widget) self.homePage_HLayout.setContentsMargins(0, 0, 0, 0) self.homePage_HLayout.setObjectName("homePage_HLayout") self.page1_pushButton1 = QtWidgets.QPushButton(self.widget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.page1_pushButton1.sizePolicy().hasHeightForWidth()) self.page1_pushButton1.setSizePolicy(sizePolicy) self.page1_pushButton1.setObjectName("page1_pushButton1") self.homePage_HLayout.addWidget(self.page1_pushButton1) self.page2_pushButton1 = QtWidgets.QPushButton(self.widget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.page2_pushButton1.sizePolicy().hasHeightForWidth()) self.page2_pushButton1.setSizePolicy(sizePolicy) self.page2_pushButton1.setObjectName("page2_pushButton1") self.homePage_HLayout.addWidget(self.page2_pushButton1) self.stackedWidget.addWidget(self.homePage) self.mainPage = QtWidgets.QWidget() self.mainPage.setObjectName("mainPage") self.subStackedWidget = QtWidgets.QStackedWidget(self.mainPage) self.subStackedWidget.setGeometry(QtCore.QRect(10, 60, 381, 291)) self.subStackedWidget.setToolTip("") self.subStackedWidget.setAccessibleName("") self.subStackedWidget.setAccessibleDescription("") self.subStackedWidget.setObjectName("subStackedWidget") self.subPage1 = QtWidgets.QWidget() self.subPage1.setObjectName("subPage1") self.subSubStackedWidget = QtWidgets.QStackedWidget(self.subPage1) self.subSubStackedWidget.setGeometry(QtCore.QRect(110, 50, 261, 181)) self.subSubStackedWidget.setObjectName("subSubStackedWidget") self.subSubPage1 = QtWidgets.QWidget() self.subSubPage1.setObjectName("subSubPage1") self.subSubPage1_someWidget = QtWidgets.QWidget(self.subSubPage1) self.subSubPage1_someWidget.setGeometry(QtCore.QRect(40, 60, 181, 61)) self.subSubPage1_someWidget.setObjectName("subSubPage1_someWidget") self.subSubPage1_someLabel = QtWidgets.QLabel(self.subSubPage1_someWidget) self.subSubPage1_someLabel.setGeometry(QtCore.QRect(20, 10, 131, 41)) self.subSubPage1_someLabel.setObjectName("subSubPage1_someLabel") self.subSubStackedWidget.addWidget(self.subSubPage1) self.subSubPage2 = QtWidgets.QWidget() self.subSubPage2.setObjectName("subSubPage2") self.subSubPage2_someWidget = QtWidgets.QWidget(self.subSubPage2) self.subSubPage2_someWidget.setGeometry(QtCore.QRect(40, 60, 181, 61)) self.subSubPage2_someWidget.setStyleSheet("background-color: rgb(0, 255, 0);") self.subSubPage2_someWidget.setObjectName("subSubPage2_someWidget") self.subSubPage2_someLabel = QtWidgets.QLabel(self.subSubPage2_someWidget) self.subSubPage2_someLabel.setGeometry(QtCore.QRect(20, 10, 131, 41)) self.subSubPage2_someLabel.setObjectName("subSubPage2_someLabel") self.subSubStackedWidget.addWidget(self.subSubPage2) self.subPage1_someWidget = QtWidgets.QWidget(self.subPage1) self.subPage1_someWidget.setGeometry(QtCore.QRect(110, 230, 271, 61)) self.subPage1_someWidget.setStyleSheet("background-color: rgb(170, 255, 255);") self.subPage1_someWidget.setObjectName("subPage1_someWidget") self.subPage1_someLabel = QtWidgets.QLabel(self.subPage1_someWidget) self.subPage1_someLabel.setGeometry(QtCore.QRect(70, 10, 131, 41)) self.subPage1_someLabel.setObjectName("subPage1_someLabel") self.widget1 = QtWidgets.QWidget(self.subPage1) self.widget1.setGeometry(QtCore.QRect(10, 10, 91, 281)) self.widget1.setObjectName("widget1") self.subPage1_VLayout = QtWidgets.QVBoxLayout(self.widget1) self.subPage1_VLayout.setContentsMargins(0, 0, 0, 0) self.subPage1_VLayout.setObjectName("subPage1_VLayout") self.subPage1_HLayout1 = QtWidgets.QHBoxLayout() self.subPage1_HLayout1.setObjectName("subPage1_HLayout1") self.search_lineEdit = QtWidgets.QLineEdit(self.widget1) self.search_lineEdit.setToolTip("") self.search_lineEdit.setStatusTip("") self.search_lineEdit.setWhatsThis("") self.search_lineEdit.setAccessibleName("") self.search_lineEdit.setText("") self.search_lineEdit.setObjectName("search_lineEdit") self.subPage1_HLayout1.addWidget(self.search_lineEdit) self.search_pushButton = QtWidgets.QPushButton(self.widget1) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.search_pushButton.sizePolicy().hasHeightForWidth()) self.search_pushButton.setSizePolicy(sizePolicy) self.search_pushButton.setMaximumSize(QtCore.QSize(25, 25)) font = QtGui.QFont() font.setPointSize(10) self.search_pushButton.setFont(font) self.search_pushButton.setObjectName("search_pushButton") self.subPage1_HLayout1.addWidget(self.search_pushButton) self.subPage1_VLayout.addLayout(self.subPage1_HLayout1) self.search_listView = QtWidgets.QListView(self.widget1) self.search_listView.setObjectName("search_listView") self.subPage1_VLayout.addWidget(self.search_listView) self.widget2 = QtWidgets.QWidget(self.subPage1) self.widget2.setGeometry(QtCore.QRect(110, 10, 271, 31)) self.widget2.setObjectName("widget2") self.subPage1_HLayout2_2 = QtWidgets.QHBoxLayout(self.widget2) self.subPage1_HLayout2_2.setContentsMargins(0, 0, 0, 0) self.subPage1_HLayout2_2.setObjectName("subPage1_HLayout2_2") self.subPage1_pushButton = QtWidgets.QPushButton(self.widget2) self.subPage1_pushButton.setObjectName("subPage1_pushButton") self.subPage1_HLayout2_2.addWidget(self.subPage1_pushButton) self.subPage2_pushButton = QtWidgets.QPushButton(self.widget2) self.subPage2_pushButton.setObjectName("subPage2_pushButton") self.subPage1_HLayout2_2.addWidget(self.subPage2_pushButton) self.subStackedWidget.addWidget(self.subPage1) self.subPage2 = QtWidgets.QWidget() self.subPage2.setObjectName("subPage2") self.subPage2_someWidget = QtWidgets.QWidget(self.subPage2) self.subPage2_someWidget.setGeometry(QtCore.QRect(70, 50, 261, 171)) self.subPage2_someWidget.setStyleSheet("background-color: rgb(170, 255, 127);") self.subPage2_someWidget.setObjectName("subPage2_someWidget") self.subPage2_someLabel = QtWidgets.QLabel(self.subPage2_someWidget) self.subPage2_someLabel.setGeometry(QtCore.QRect(80, 60, 131, 41)) self.subPage2_someLabel.setObjectName("subPage2_someLabel") self.subStackedWidget.addWidget(self.subPage2) self.widget3 = QtWidgets.QWidget(self.mainPage) self.widget3.setGeometry(QtCore.QRect(10, 0, 381, 51)) self.widget3.setObjectName("widget3") self.mainPage_HLayout = QtWidgets.QHBoxLayout(self.widget3) self.mainPage_HLayout.setContentsMargins(0, 0, 0, 0) self.mainPage_HLayout.setObjectName("mainPage_HLayout") self.page1_pushButton2 = QtWidgets.QPushButton(self.widget3) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.page1_pushButton2.sizePolicy().hasHeightForWidth()) self.page1_pushButton2.setSizePolicy(sizePolicy) self.page1_pushButton2.setObjectName("page1_pushButton2") self.mainPage_HLayout.addWidget(self.page1_pushButton2) self.page2_pushButton2 = QtWidgets.QPushButton(self.widget3) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.page2_pushButton2.sizePolicy().hasHeightForWidth()) self.page2_pushButton2.setSizePolicy(sizePolicy) self.page2_pushButton2.setObjectName("page2_pushButton2") self.mainPage_HLayout.addWidget(self.page2_pushButton2) self.home_pushButton = QtWidgets.QPushButton(self.widget3) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.home_pushButton.sizePolicy().hasHeightForWidth()) self.home_pushButton.setSizePolicy(sizePolicy) self.home_pushButton.setObjectName("home_pushButton") self.mainPage_HLayout.addWidget(self.home_pushButton) self.stackedWidget.addWidget(self.mainPage) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 400, 21)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.retranslateUi(MainWindow) self.stackedWidget.setCurrentIndex(0) self.subStackedWidget.setCurrentIndex(0) self.subSubStackedWidget.setCurrentIndex(0) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.page1_pushButton1.setText(_translate("MainWindow", "Page 1")) self.page2_pushButton1.setText(_translate("MainWindow", "Page 2")) self.subSubPage1_someLabel.setText(_translate("MainWindow", "Some Widget SubPage 1")) self.subSubPage2_someLabel.setText(_translate("MainWindow", "Some Widget SubPage 2")) self.subPage1_someLabel.setText(_translate("MainWindow", "Some Widget Page 1")) self.search_lineEdit.setPlaceholderText(_translate("MainWindow", "Search")) self.search_pushButton.setText(_translate("MainWindow", "►")) self.subPage1_pushButton.setText(_translate("MainWindow", "Sub Page1")) self.subPage2_pushButton.setText(_translate("MainWindow", "Sub page 2")) self.subPage2_someLabel.setText(_translate("MainWindow", "Some Widget Page 2")) self.page1_pushButton2.setText(_translate("MainWindow", "Page 1")) self.page2_pushButton2.setText(_translate("MainWindow", "Page 2")) self.home_pushButton.setText(_translate("MainWindow", "Home"))
main.py
import sys from PyQt5 import QtWidgets from main_UI import Ui_MainWindow class Main(QtWidgets.QMainWindow): def __init__(self): super(Main, self).__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.stackedWidget.setCurrentIndex(0) self.ui.page1_pushButton1.clicked.connect(lambda: self.show_stacked_widget(1)) self.ui.page1_pushButton1.clicked.connect(lambda: self.show_sub_stacked_widget(0)) self.ui.page2_pushButton1.clicked.connect(lambda: self.show_stacked_widget(1)) self.ui.page2_pushButton1.clicked.connect(lambda: self.show_sub_stacked_widget(1)) self.ui.page1_pushButton2.clicked.connect(lambda: self.show_sub_stacked_widget(0)) self.ui.page2_pushButton2.clicked.connect(lambda: self.show_sub_stacked_widget(1)) self.ui.home_pushButton.clicked.connect(lambda: self.show_stacked_widget(0)) self.ui.subPage1_pushButton.clicked.connect(lambda: self.show_sub_sub_stacked_widget(0)) self.ui.subPage2_pushButton.clicked.connect(lambda: self.show_sub_sub_stacked_widget(1)) def show_stacked_widget(self, index): self.ui.stackedWidget.setCurrentIndex(index) def show_sub_stacked_widget(self, index): self.ui.subStackedWidget.setCurrentIndex(index) def show_sub_sub_stacked_widget(self, index): self.ui.subSubStackedWidget.setCurrentIndex(index) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) application = Main() application.show() sys.exit(app.exec_())