create an MDI screen with Python and Pyqt5

Asked

Viewed 318 times

0

I’m making a system with Python and Pyqt5. I already created the menus and now I’m applying the necessary functions of each item.

I want to work with MDI screens and I’m having difficulties with it.

I am working with separate folder structure for each module and its classes.

When I create my script to create the MDI screen all in a single script, without separating the classes, it works, creates the right screen, but when having to do this to my main script is not working, I believe that because of the dependencies, I do not know.

Could someone help me tweak my code to work perfectly creating the screen of "Create SCD"?

"main"

# coding: utf-8

import sys
import lib.mainMenu as Menu
import lib.mainToolBar as MainToolbar

from lib.config import path_file
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox, QMdiArea
from PyQt5.QtCore import QSize
from PyQt5 import QtGui

__version__ = "1.0.0"

image_root = path_file()
window_icon = image_root + r"\public\icons\etl.png"

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.setMinimumSize(QSize(1200, 800))
        self.setWindowIcon(QtGui.QIcon(window_icon))
        self.title = 'System Converter Database - Version: '
        self.setWindowTitle(self.title + __version__)
        self.move(app.desktop().screen().rect().center() - self.rect().center())

        # Criando o Widget central
        self.mdi_area = QMdiArea()
        self.setCentralWidget(self.mdi_area)

        Menu.create_main_menu(self)
        MainToolbar.create_toolbar(self)

        self.statusBar().showMessage('Ready')

        self.show()

    def closeEvent(self, event):
        quit_msg = "Deseja mesmo sair do sistema?"
        reply = QMessageBox.question(self, "Saindo do sistema", quit_msg, QMessageBox.Yes, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

"menu module"

# coding: utf-8

from lib.config import path_file
from PyQt5.QtWidgets import QAction, QMessageBox, qApp, QWidget
from PyQt5.QtGui import QIcon
from PyQt5 import QtGui
from PyQt5.QtCore import QSize


image_root = path_file()


class MdiChild(QWidget):
    def __init__(self):
        super().__init__()


def create_main_menu(handle):
    # Action do menu Criar SCD
    criar_scd_icon = image_root + r"\public\icons\Criar_scd.png"
    action_criar_scd = QAction(QIcon(criar_scd_icon), "C&riar SCD", handle)
    action_criar_scd.setShortcut("Ctrl+Shift+R")
    action_criar_scd.triggered.connect(criar_scd)

    # Action do menu Localizar SCD
    localizar_scd_icon = image_root + r"\public\icons\localizar.png"
    action_localizar_scd = QAction(QIcon(localizar_scd_icon), "&Localizar SCD", handle)
    action_localizar_scd.setShortcut("Ctrl+Shift+L")
    action_localizar_scd.triggered.connect(localizar_scd)

    # Action do menu Conversor/passo a passo/copiar
    copiar_icon = image_root + r"\public\icons\copiar.png"
    action_copiar = QAction(QIcon(copiar_icon), "&Copiar", handle)
    action_copiar.setShortcut("Ctrl+Shift+C")
    action_copiar.triggered.connect(copiar)

    # Action do menu Conversor/passo a passo/extrair
    extrair_icon = image_root + r"\public\icons\extrair.png"
    action_extrair = QAction(QIcon(extrair_icon), "&Extrair", handle)
    action_extrair.setShortcut("Ctrl+Shift+E")
    action_extrair.triggered.connect(extrair)

    # Action do menu Conversor/passo a passo/Preparar
    preparar_icon = image_root + r"\public\icons\preparar.png"
    action_preparar = QAction(QIcon(preparar_icon), "&Preparar", handle)
    action_preparar.setShortcut("Ctrl+Shift+P")
    action_preparar.triggered.connect(preparar)

    # Action do menu Conversor/passo a passo/converter
    converter_icon = image_root + r"\public\icons\converter.png"
    action_converter = QAction(QIcon(converter_icon), "Co&nverter", handle)
    action_converter.setShortcut("Ctrl+Shift+N")
    action_converter.triggered.connect(converter)

    # Action do menu Conversor/automático/iniciar
    automatico_iniciar_icon = image_root + r"\public\icons\automatico.png"
    action_automatico_iniciar = QAction(QIcon(automatico_iniciar_icon), "&Iniciar", handle)
    action_automatico_iniciar.setShortcut("Ctrl+Shift+I")
    action_automatico_iniciar.triggered.connect(automatico_iniciar)

    # Action do menu Verificador/iniciar
    verificador_iniciar_icon = image_root + r"\public\icons\automatico.png"
    action_verificador_iniciar = QAction(QIcon(verificador_iniciar_icon), "Veri&ficador", handle)
    action_verificador_iniciar.setShortcut("Ctrl+Shift+F")
    action_verificador_iniciar.triggered.connect(verificador_iniciar)

    # Action do menu sair
    sair_icon = image_root + r"\public\icons\exit.png"
    action_sair = QAction(QIcon(sair_icon), "&Sair", handle)
    action_sair.setShortcut("Ctrl+Shift+S")
    action_sair.triggered.connect(sair)

    # Criando barra de menu principal
    main_menu = handle.menuBar()
    menu_scd = main_menu.addMenu("SC&D")
    menu_converter = main_menu.addMenu("C&onversor")
    menu_converter_automatico = menu_converter.addMenu("&Automático")
    menu_converter_passo = menu_converter.addMenu("&Passo a passo")
    menu_verificador = main_menu.addMenu("&Verificador")
    menu_sair = main_menu.addMenu("&Sair")

    # Criando os sub-menus
    menu_scd.addAction(action_criar_scd)
    menu_scd.addAction(action_localizar_scd)
    menu_sair.addAction(action_sair)
    menu_converter_passo.addAction(action_copiar)
    menu_converter_passo.addAction(action_extrair)
    menu_converter_passo.addAction(action_preparar)
    menu_converter_passo.addAction(action_converter)
    menu_converter_automatico.addAction(action_automatico_iniciar)
    menu_verificador.addAction(action_verificador_iniciar)


def create_mdi_child(parent=None):
    if parent is not None:
        child = MdiChild()
        parent.mdi_area.addSubWindow(child)

        return child


def criar_scd(parent=None):
    tela_scd = parent.create_mdi_child()
    tela_scd.setWindowTitle("Criar SCD")
    tela_scd.setMinimumSize(QSize(1200, 800))
    tela_scd.show()

    print("Criar SCD")


def localizar_scd():
    print("Localizar SCD")


def copiar():
    pass


def extrair():
    pass


def preparar():
    pass


def converter():
    pass


def automatico_iniciar():
    pass


def verificador_iniciar():
    pass


def sair():
    box_quit = QMessageBox()
    box_quit.setWindowIcon(QtGui.QIcon(image_root + r"\public\icons\etl.png"))
    box_quit.setIcon(QMessageBox.Question)
    box_quit.setWindowTitle("Saindo do sistema")
    box_quit.setText("Deseja mesmo sair do sistema?")
    box_quit.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
    box_quit.setDefaultButton(QMessageBox.No)

    reply = box_quit.exec_()
    if reply == QMessageBox.Yes:
        qApp.quit()

When I click on the "Create SCD" menu the system does not create the MDI screen that should be created and closes the system.

I need help to bring up this MDI screen, please.

If I do it this way below, it works, it creates the MDI window successfully, but I don’t know how to adapt it to my reality.

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.centralwidget = QtWidgets.QWidget(self)
        self.setCentralWidget(self.centralwidget)
        self.centralwidget.setLayout(QtWidgets.QVBoxLayout(self.centralwidget))

        self.mdiArea = QtWidgets.QMdiArea(self.centralwidget)
        self.centralwidget.layout().addWidget(self.mdiArea)

        subWindow = QtWidgets.QMdiSubWindow(self)

        widget = QtWidgets.QWidget()
        widget.setLayout(QtWidgets.QVBoxLayout())
        btn = QtWidgets.QPushButton("close", widget) 
        widget.layout().addWidget(btn)

        btn.clicked.connect(subWindow.close)

        subWindow.setWidget(widget)
        subWindow.setObjectName("New_Window")
        subWindow.setWindowTitle("New SubWindow")
        self.mdiArea.addSubWindow(subWindow)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

Thanks for your help.

Does anyone have any ideas on how to resolve this situation?

1 answer

0


I was able to solve the problem mentioned above. I’ll post here how I managed to do what I wanted. All part is done in the imported view module below:

from view.view_main import MainWindow

"main"

class AppMainWindow(MainWindow):
    def __init__(self, ):
        super(AppMainWindow, self).__init__()

        self.move(app.desktop().screen().rect().center() - self.rect().center())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = AppMainWindow()
    ex.show()
    sys.exit(app.exec_())

"Modulo View"

# coding: utf-8

import sys

from lib.config import path_file
from PyQt5.QtWidgets import QMainWindow, QMdiArea, QMessageBox, QAction, QMdiSubWindow, QWidget
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QIcon, QKeySequence

__version__ = "1.0.0"

image_root = path_file()
window_icon = image_root + r"\public\icons\etl.png"


class MainWindow(QMainWindow):
    child_count = 0

    def __init__(self, ):
        super(MainWindow, self).__init__()
        self.setMinimumSize(QSize(1200, 800))
        self.title = 'System Converter Database - Version: '
        self.setWindowIcon(QIcon(window_icon))
        self.setWindowTitle(self.title + __version__)

        #Criando o widget central
        self.mdi_widget = QMdiArea()
        self.setCentralWidget(self.mdi_widget)

        self.create_actions_menu()
        self.create_main_menu()


    def closeEvent(self, event):
        quit_msg = "Deseja mesmo sair do sistema?"
        reply = QMessageBox.question(self, "Saindo do sistema", quit_msg, QMessageBox.Yes, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

    def create_main_menu(self):
        main_menu = self.menuBar()  # Barra de Menus
        self.menu_scd = main_menu.addMenu("&SCD")  # Menu SCD
        self.menu_scd.addAction(self.action_create_scd)  # SubMenu "Criar SCD"
        self.menu_scd.addAction(self.action_search_scd)


    def create_actions_menu(self):
        # Ação para Menu "Criar SCD"
        create_scd_icon = image_root + r"\public\icons\Criar_scd.png"
        self.action_create_scd = QAction(QIcon(create_scd_icon), "&Criar", self,
                                         shortcut=QKeySequence("Ctrl+Shift+C"),
                                         triggered=self.create_scd)
        # Ação para Menu "Localizar SCD"
        search_scd_icon = image_root + r"\public\icons\localizar.png"
        self.action_search_scd = QAction(QIcon(search_scd_icon), "&Localizar", self,
                                         shortcut=QKeySequence("Ctrl+Shift+L"),
                                         triggered=self.search_scd)

    def create_scd(self):
        self.create_child_window()
        print("Criar SCD")

    def search_scd(self):
        print("Localizar SCD")

    def create_child_window(self):
        sub = QMdiSubWindow()
        sub.setWidget(QWidget())
        sub.setWindowTitle("teste")
        self.mdi_widget.addSubWindow(sub)
        sub.show()

The cat jump was on account of the code pad below:

def create_child_window(self):
            sub = QMdiSubWindow()
            sub.setWidget(QWidget())
            sub.setWindowTitle("teste")
            self.mdi_widget.addSubWindow(sub)
            sub.show()

It turns out that before, in the above question I was not succeeding, as I was not succeeding in instantiating, or inheriting from a Qwindow type object.

Thanks for all the views and help attempts. Now it’s just me to improve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.