Jump to content
Fórum Script Brasil
  • 0

Como criar uma tela MDI com Python e PyQT5


Question

Estou fazendo um sistema com Python e PyQT5. Já criei os menus e agora estou aplicando as funções necessárias de cada item.

Quero trabalhar com telas MDI e estou tendo dificuldades com isso.

Estou trabalhando com estrutura de pastas separadas para cada módulo e suas classes.

Quando eu crio meu script para criar a tela MDI tudo em um único script, sem separar as classes, funciona, cria a tela certinho, mas quando tendo fazer isso para o meu script principal não está funcionando, acredito que por causa das dependências, não sei.

Alguém poderia me ajudar a ajustar meu código para funcionar perfeitamente a criação da tela de "Criar 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_())

"Modulo menus"

# 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()

Quando clico no menu "Criar SCD" o sistema não cria a tela MDI que deveria ser criada e fecha o sistema.

Preciso de ajuda para fazer aparecer essa tela MDI, por favor.

Se eu faço desse jeito abaixo, dá certo, ele cria a janela MDI com sucesso, mas não sei como fazer para adaptar isso a minha realidade.

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_())

Desde já agradeço pela ajuda.

Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      148681
    • Total Posts
      644506
×
×
  • Create New...