Задать вопрос
@orlatym
я очень тупой

Могу ли я хранить все конопки, виджеты и текстреи в отдельном файле?

У меня в шаблоне есть повторяющиеся куски кода и что бы оптимизировать код я хочу разделить шаблон на несколько частей и загружать в зависимости от того что запрашивает пользователь.
Например я хочу вставить кнопки #but_1, #but_1, #but_2, #but_1 из test2 во внутрь #smile_window_tes. слева от #label
test1.py
from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
    QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QFont,
    QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
    QRadialGradient)
from PySide2.QtWidgets import *

class Ui_Windows_main(object):
    def setupUi(self, Windows_main):
        if Windows_main.objectName():
            Windows_main.setObjectName(u"Windows_main")
        Windows_main.resize(764, 604)
        Windows_main.setStyleSheet(u"\n"
"\n"
"#Test_window #Window_test{\n"
"background-color: rgb(239, 41, 41);\n"
"}
"#Test_window #smile_window_tes{\n"
"background-color: #fff;\n"
"}\n"
"")
        self.Test_window = QWidget(Windows_main)
        self.Test_window.setObjectName(u"Test_window")
        self.Test_window.setEnabled(True)
        self.Test_window.setTabletTracking(False)
        self.gridLayout = QGridLayout(self.Test_window)
        self.gridLayout.setObjectName(u"gridLayout")
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalSpacer = QSpacerItem(20, 142, QSizePolicy.Minimum, QSizePolicy.Expanding)

        self.gridLayout.addItem(self.verticalSpacer, 0, 1, 1, 1)

        self.horizontalSpacer = QSpacerItem(254, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

        self.gridLayout.addItem(self.horizontalSpacer, 1, 0, 1, 1)

        self.Window_test = QWidget(self.Test_window)
        self.Window_test.setObjectName(u"Window_test")
        self.Window_test.setMinimumSize(QSize(250, 250))
        self.Window_test.setStyleSheet(u"")
        self.verticalLayout = QVBoxLayout(self.Window_test)
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.Text = QLabel(self.Window_test)
        self.Text.setObjectName(u"Text")
        font = QFont()
        font.setPointSize(21)
        self.Text.setFont(font)
        self.Text.setAlignment(Qt.AlignCenter)

        self.verticalLayout.addWidget(self.Text)

        self.smile_window_tes = QWidget(self.Window_test)
        self.smile_window_tes.setObjectName(u"smile_window_tes")
        self.gridLayout_2 = QGridLayout(self.smile_window_tes)
        self.gridLayout_2.setObjectName(u"gridLayout_2")
        self.label = QLabel(self.smile_window_tes)
        self.label.setObjectName(u"label")

        self.gridLayout_2.addWidget(self.label, 0, 0, 1, 1)


        self.verticalLayout.addWidget(self.smile_window_tes)

        self.verticalLayout.setStretch(1, 1)

        self.gridLayout.addWidget(self.Window_test, 1, 1, 1, 1)

        self.verticalSpacer_2 = QSpacerItem(20, 141, QSizePolicy.Minimum, QSizePolicy.Expanding)

        self.gridLayout.addItem(self.verticalSpacer_2, 2, 1, 1, 1)

        self.horizontalSpacer_2 = QSpacerItem(254, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

        self.gridLayout.addItem(self.horizontalSpacer_2, 1, 2, 1, 1)

        self.gridLayout.setRowStretch(0, 1)
        self.gridLayout.setRowStretch(2, 1)
        self.gridLayout.setColumnStretch(0, 1)
        self.gridLayout.setColumnStretch(2, 1)
        Windows_main.setCentralWidget(self.Test_window)
        self.menubar = QMenuBar(Windows_main)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 764, 22))
        Windows_main.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(Windows_main)
        self.statusbar.setObjectName(u"statusbar")
        Windows_main.setStatusBar(self.statusbar)

        self.retranslateUi(Windows_main)

        QMetaObject.connectSlotsByName(Windows_main)
    # setupUi

    def retranslateUi(self, Windows_main):
        Windows_main.setWindowTitle(QCoreApplication.translate("Windows_main", u"MainWindow", None))
        self.Text.setText(QCoreApplication.translate("Windows_main", u"Text", None))
        self.label.setText(QCoreApplication.translate("Windows_main", u"TextLabel", None))
    # retranslateUi


test2.py
from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
    QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QFont,
    QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
    QRadialGradient)
from PySide2.QtWidgets import *

class Ui_array_but(object):
    def setupUi(self, array_but):
        if array_but.objectName():
            array_but.setObjectName(u"array_but")
        array_but.resize(800, 583)
        array_but.setStyleSheet(u"#but_1{\n"
"background:rgb(243, 255, 11)\n"
"}\n"
"\n"
"#but_2{\n"
"background:rgb(56, 255, 239)\n"
"}")
        self.centralwidget = QWidget(array_but)
        self.centralwidget.setObjectName(u"centralwidget")
        self.but_1 = QRadioButton(self.centralwidget)
        self.but_1.setObjectName(u"but_1")
        self.but_1.setGeometry(QRect(0, 10, 83, 18))
        self.but_2 = QRadioButton(self.centralwidget)
        self.but_2.setObjectName(u"but_2")
        self.but_2.setGeometry(QRect(0, 40, 83, 18))
        array_but.setCentralWidget(self.centralwidget)
        self.menubar = QMenuBar(array_but)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 800, 22))
        array_but.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(array_but)
        self.statusbar.setObjectName(u"statusbar")
        array_but.setStatusBar(self.statusbar)

        self.retranslateUi(array_but)

        QMetaObject.connectSlotsByName(array_but)
    # setupUi

    def retranslateUi(self, array_but):
        array_but.setWindowTitle(QCoreApplication.translate("array_but", u"MainWindow", None))
        self.but_1.setText(QCoreApplication.translate("array_but", u"But 1", None))
        self.but_2.setText(QCoreApplication.translate("array_but", u"But 2", None))
    # retranslateUi

Главный файл main.py
from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
    QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QFont,
    QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
    QRadialGradient)
from PySide2.QtWidgets import *

from PySide2.QtWidgets import QApplication,QWidget
import sys


from test1 import Ui_Windows_main
from test2 import Ui_array_but

class QtMainWindow(QMainWindow, Ui_Windows_main):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)

if __name__ == '__main__':

    my = QApplication(sys.argv)
    window = QtMainWindow()
    window.show()

    sys.exit(my.exec_())

Как это сделать и вызовет ли это какие либо ошибки или конфликты имен?
Я только начал изучать pyside поэтому и спрашиваю.
  • Вопрос задан
  • 46 просмотров
Подписаться 1 Простой Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы