GUI на PyQt и вызов с другого файла

Понимаю, что в своем проекте наворотил все и сразу, типа - пытаюсь получить сумму "а" и "2" и удивляюсь, почему ошибка))

Но , к сути... В самое начало.
Есть два файла - условно назовем их primary.py и secondary.py. Файл secondary.py хочу написать как модуль, чтобы можно было его вызвать в primary.py.

secondary.py:

import sys
from PyQt5 import QtCore, QtWidgets, QtGui
import images                              # файл дизайна


class Images(QtWidgets.QMainWindow, images.Ui_MainWindow):
   def __init__(self, parent=None):
      QtWidgets.QWidget.__init__(self, parent)
      self.setupUi(self)


def run_module_images(caption):
   imagin = QtWidgets.QApplication(sys.argv)
   window = Images()  # Создаем  экземпляр  класса
   window.setWindowTitle(caption)
   if caption == "Join Images":
      window.cnsPic4.setVisible(False)
   elif caption == "Choise Image":
      icon = QtGui.QIcon()
      icon.addPixmap(QtGui.QPixmap(":/Icon/Choise.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
      window.setWindowIcon(icon)
      window.resize(246, 570)
      window.cnsList1.resize(230, 400)
      window.cnsPic4.setVisible(True)
   window.show()  # Отображаем  окно
   sys.exit(imagin.exec_())  # Запускаем  цикл  обработки  событий


if __name__ == '__main__':
   run_module_images("Join Images")

primary.py:

import sys
from PyQt5 import QtWidgets, QtGui
from secondary import Images


imagin = QtWidgets.QApplication(sys.argv)
window = Images()  # Создаем  экземпляр  класса
window.setWindowTitle("Choise Image")
if window.windowTitle() == "Join Images":
    window.cnsPic4.setVisible(False)
elif window.windowTitle() == "Choise Image":
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(":/Icon/Choise.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    window.setWindowIcon(icon)
    window.resize(246, 570)
    window.cnsList1.resize(230, 400)
    window.cnsPic4.setVisible(True)
window.show()  # Отображаем  окно
sys.exit(imagin.exec_())  # Запускаем  цикл  обработки  событий

В таком виде оно работает, но...

  1. Если я импортирую seconrary, в котором уже импортирован PyQt5, то почему в primary его надо импортировать повторно (как и sys).
  2. Как избежать такой громоздкости (хотя это наверное моя криворукость) ?

Если можно, прошу показать правильную модель на этом конкретном примере. Типа - что должно быть в модуле, и в основном файле.

Нарисованный в Qt Designer дизайн, сконвертированный потом в images.py:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowModality(QtCore.Qt.WindowModal)
        MainWindow.resize(788, 570)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/Icon/Join.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setToolTip("")
        MainWindow.setStatusTip("")
        MainWindow.setWhatsThis("")
        MainWindow.setAccessibleName("")
        MainWindow.setAccessibleDescription("")
        MainWindow.setWindowFilePath("")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.cnsList1 = QtWidgets.QListWidget(self.centralwidget)
        self.cnsList1.setGeometry(QtCore.QRect(8, 8, 230, 530))
        self.cnsList1.setObjectName("cnsList1")
        self.cnsButton1 = QtWidgets.QPushButton(self.centralwidget)
        self.cnsButton1.setGeometry(QtCore.QRect(8, 540, 230, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.cnsButton1.setFont(font)
        self.cnsButton1.setToolTip("")
        self.cnsButton1.setStatusTip("")
        self.cnsButton1.setWhatsThis("")
        self.cnsButton1.setAccessibleName("")
        self.cnsButton1.setAccessibleDescription("")
        self.cnsButton1.setText("Открыть")
        self.cnsButton1.setObjectName("cnsButton1")
        self.cnsPic1 = QtWidgets.QLabel(self.centralwidget)
        self.cnsPic1.setGeometry(QtCore.QRect(250, 30, 250, 250))
        self.cnsPic1.setToolTip("")
        self.cnsPic1.setStatusTip("")
        self.cnsPic1.setWhatsThis("")
        self.cnsPic1.setAccessibleName("")
        self.cnsPic1.setAccessibleDescription("")
        self.cnsPic1.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.cnsPic1.setLineWidth(3)
        self.cnsPic1.setMidLineWidth(2)
        self.cnsPic1.setText("")
        self.cnsPic1.setScaledContents(True)
        self.cnsPic1.setObjectName("cnsPic1")
        self.cnsPic2 = QtWidgets.QLabel(self.centralwidget)
        self.cnsPic2.setGeometry(QtCore.QRect(530, 30, 250, 250))
        self.cnsPic2.setToolTip("")
        self.cnsPic2.setStatusTip("")
        self.cnsPic2.setWhatsThis("")
        self.cnsPic2.setAccessibleName("")
        self.cnsPic2.setAccessibleDescription("")
        self.cnsPic2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.cnsPic2.setFrameShadow(QtWidgets.QFrame.Plain)
        self.cnsPic2.setLineWidth(3)
        self.cnsPic2.setMidLineWidth(2)
        self.cnsPic2.setText("")
        self.cnsPic2.setScaledContents(True)
        self.cnsPic2.setObjectName("cnsPic2")
        self.cnsPic3 = QtWidgets.QLabel(self.centralwidget)
        self.cnsPic3.setGeometry(QtCore.QRect(265, 310, 500, 250))
        self.cnsPic3.setToolTip("")
        self.cnsPic3.setStatusTip("")
        self.cnsPic3.setWhatsThis("")
        self.cnsPic3.setAccessibleName("")
        self.cnsPic3.setAccessibleDescription("")
        self.cnsPic3.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.cnsPic3.setLineWidth(3)
        self.cnsPic3.setMidLineWidth(2)
        self.cnsPic3.setText("")
        self.cnsPic3.setScaledContents(True)
        self.cnsPic3.setObjectName("cnsPic3")
        self.cnsLabel1 = QtWidgets.QLabel(self.centralwidget)
        self.cnsLabel1.setGeometry(QtCore.QRect(250, 8, 250, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.cnsLabel1.setFont(font)
        self.cnsLabel1.setToolTip("")
        self.cnsLabel1.setStatusTip("")
        self.cnsLabel1.setWhatsThis("")
        self.cnsLabel1.setAccessibleName("")
        self.cnsLabel1.setAccessibleDescription("")
        self.cnsLabel1.setObjectName("cnsLabel1")
        self.cnsLabel2 = QtWidgets.QLabel(self.centralwidget)
        self.cnsLabel2.setGeometry(QtCore.QRect(530, 8, 250, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.cnsLabel2.setFont(font)
        self.cnsLabel2.setToolTip("")
        self.cnsLabel2.setStatusTip("")
        self.cnsLabel2.setWhatsThis("")
        self.cnsLabel2.setAccessibleName("")
        self.cnsLabel2.setAccessibleDescription("")
        self.cnsLabel2.setObjectName("cnsLabel2")
        self.cnsButton3 = QtWidgets.QPushButton(self.centralwidget)
        self.cnsButton3.setGeometry(QtCore.QRect(500, 30, 30, 250))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.cnsButton3.setFont(font)
        self.cnsButton3.setToolTip("")
        self.cnsButton3.setStatusTip("")
        self.cnsButton3.setWhatsThis("")
        self.cnsButton3.setAccessibleName("")
        self.cnsButton3.setAccessibleDescription("")
        self.cnsButton3.setText("<>")
        self.cnsButton3.setObjectName("cnsButton3")
        self.cnsButton2 = QtWidgets.QPushButton(self.centralwidget)
        self.cnsButton2.setGeometry(QtCore.QRect(250, 285, 530, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.cnsButton2.setFont(font)
        self.cnsButton2.setToolTip("")
        self.cnsButton2.setStatusTip("")
        self.cnsButton2.setWhatsThis("")
        self.cnsButton2.setAccessibleName("")
        self.cnsButton2.setAccessibleDescription("")
        self.cnsButton2.setText("Сохранить")
        self.cnsButton2.setObjectName("cnsButton2")
        self.cnsPic4 = QtWidgets.QLabel(self.centralwidget)
        self.cnsPic4.setEnabled(True)
        self.cnsPic4.setGeometry(QtCore.QRect(8, 410, 230, 120))
        self.cnsPic4.setToolTip("")
        self.cnsPic4.setStatusTip("")
        self.cnsPic4.setWhatsThis("")
        self.cnsPic4.setAccessibleName("")
        self.cnsPic4.setAccessibleDescription("")
        self.cnsPic4.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.cnsPic4.setFrameShadow(QtWidgets.QFrame.Plain)
        self.cnsPic4.setLineWidth(3)
        self.cnsPic4.setMidLineWidth(2)
        self.cnsPic4.setText("")
        self.cnsPic4.setScaledContents(True)
        self.cnsPic4.setObjectName("cnsPic4")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.cnsLabel1.setText(_translate("MainWindow", "Аверс"))
        self.cnsLabel2.setText(_translate("MainWindow", "Реверс"))
import resource_rc


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Хочу понять общую модель вызова окна с файла secondary в другом файле (или с другого окна).


Ответы (0 шт):