简体   繁体   中英

PyQt - how to stop execution without closing the dialog window

I have one dialog window with various linEdits and buttons which call various functions. One button sends various signals and when an exception/error occurs, it closes the whole window. I have found some similar cases, but this one is specific in having various signals.

Now this is the code where I want to check if the path in line edit and the files exist. If not I would like to display the message and keep the dialog window open. So handle the error without executing further signals and closing the window. sys.exit() unfortunately closes the whole window.

This is what I have so far:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog, QLabel, QCheckBox, QWidget, QMessageBox
from os.path import expanduser

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(450, 39)
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(120, 10, 311, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(20, 10, 75, 23))
        self.pushButton.setObjectName("pushButton")

        self.pushButton.clicked.connect(self.checkfolder)
        self.pushButton.clicked.connect(self.checkfilexist)        
        self.pushButton.clicked.connect(self.Runnormal)


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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.pushButton.setText(_translate("Dialog", "Click"))



    def checkfolder(self):
        try:
            import sys
            import os
            import glob
            import ctypes
            didi = self.lineEdit.text()
            if  os.path.exists(didi):
                print(didi)
                pass
            elif not os.path.exists(didi):
                ctypes.windll.user32.MessageBoxW(0, "Enter the existing path!", "ERROR", 1)
                return

        except Exception as exc:
            traceback.print_exc()
            raw_input()


    def checkfilexist(self):
        try:
            import sys
            import os
            import glob
            import ctypes
            didi = self.lineEdit.text()
            fufu = didi + '/' + '*USR02.txt'
            if  glob.glob(fufu):
                print(didi)
                pass
            elif not os.path.isfile(fufu):
                ctypes.windll.user32.MessageBoxW(0, "No files found, please try again!", "ERROR", 1)
                return

        except Exception as exc:
            traceback.print_exc()
            raw_input()


#clean the files to obtain headers
    def Runnormal(self):
        try:
            import os
            bad_words = {'--------', 'Table:', 'Displayed Fields:', 'Dynamic List Display'}
            didi = self.lineEdit.text()
#            print(didi)
            for filename in os.listdir(didi):            
                    if filename.endswith("DEVACCESS.txt"):
#                        print(filename)
                        filepath = os.path.join(didi, filename)
                        with open(filepath, errors='ignore') as oldfile, open(didi + "\\clean_" + filename, 'w') as newfile:
#                            print(oldfile)
#                            print(newfile)
                            for line in oldfile:
                                if not any(bad_word in line for bad_word in bad_words):
                                    newfile.write(line)

        except Exception as exc:
            traceback.print_exc()
            raw_input()


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys._excepthook = sys.excepthook 
    sys.exit(app.exec_())

When the path does not exist, or the file exists - the code does not stop, but passes to the third signal and fails/closes the window.

How to handle errors/stop executing script without closing the dialog window or not passing to the other button signals? Basically, stop after the first or second signal without closing the whole window.

Let's say that the first two defs or signals are enough to meet the condition, can this actually work? Calling the signal from the def checkfilexist and let the next signal call the subsequent one or just give an error message.

def checkfilexist(self):
    try:
        import sys
        import os
        import glob
        import ctypes
        didi = self.lineEdit.text()
        fufu = didi + '/' + '*USR02.txt'
        if  glob.glob(fufu):
            self.pushButton.clicked.connect(self.Runnormal)

        elif not os.path.isfile(fufu):
            ctypes.windll.user32.MessageBoxW(0, "No files found, please try again!", "ERROR", 1)
            return

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM