Display message (QmessageBox) from another class other than the main one in PyQt4

2

Friends, I ask you for help, I suppose it's simple but I'm having difficulty, because I'm still learning object orientation with PyQt4. I have a main class (Application) separate from the class that forms the main screen (in the ui_principal.py file) of the system and another (TrataDiretorio) that has specific function of verifying if the path of a directory exists and if it exists, it creates a folder inside of it, but I want to make an error message appear in a window (widget) if there is no path. Here is the code below:

Main class in file (weekly_spy.py)

from PyQt4.QtGui import  (QApplication,QMessageBox)
from PyQt4.QtCore import (SIGNAL, QTimer, QString)
#-------------------------
from ui_principal import *
from tratar_diretorio import *

class Aplicacao(QtGui.QMainWindow, Ui_JanelaPrincipal):
    def __init__(self, parent=None):
        super(Aplicacao, self).__init__(parent)
        self.setupUi(self)
        self.animacao_titulo()
        self.pushButton_sair.clicked.connect(self.sair)
        self.pushButton_processar.clicked.connect(self.processar_sistema)
    #----------------------------------------
    def sair(self):
        sys.exit()
    #----------------------------------------
    def processar_sistema(self):
        c = TrataDiretorio()
        if c.verificar_criar_diretorio():
            self.pushButton_processar.setEnabled(False)
#------------------------------
if __name__ == "__main__":
    app = QApplication([sys.argv])
    app.processEvents()
    w = Aplicacao()
    w.show()
    w.raise_()
    sys.exit(app.exec_())  

Class AddressDirectory in file (treat_dir.py)

import sys
import os.path
import time
#--------------------
class TrataDiretorio():
    def __init__(self):
        self.data                = None
        self.pasta               = None
        self.diretorio           = None
        self.status              = True
        self.log                 = open('log.txt', 'w+')
    #---------------------------------------
    def verificar_criar_diretorio(self):
        try:
            if os.path.isdir(self.diretorio): # vemos se este dir já existe
                os.mkdir(self.pasta)
                time.sleep(2)
                self.log.write('verificar_criar_diretorio: \n')
                self.log.write('-----------------------------\n')
                self.status = True
                return self.status
            else:
======>         QMessageBox.information(self, u"Informação", u'O caminho <b>"%s"</b> para criação da pasta <b>"%s"</b> não existe!'%(self.diretorio,self.data))
                self.status = False
                return self.status              
        except Exception as erro:
            self.log.write('verificar_criar_diretorio: \n%s\n'%erro)
======>     QMessageBox.critical(self, "Erro", erro)
            self.status = False
            return self.status  
    
asked by anonymous 21.11.2017 / 18:45

0 answers