PyQt-如何在不关闭对话框窗口的情况下停止执行

时间:2019-12-05 18:14:00

标签: python pyqt5

我有一个对话框窗口,其中包含各种linEdits和调用各种功能的按钮。 一个按钮发送各种信号,当发生异常/错误时,它将关闭整个窗口。 我发现了一些类似的情况,但这是特定于各种信号的情况。

现在这是我要检查行编辑路径和文件是否存在的代码。如果不是,我想显示该消息并使对话窗口保持打开状态。因此,无需执行其他信号并关闭窗口即可处理错误。 sys.exit()很不幸地关闭了整个窗口。

这是我到目前为止所拥有的:

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

当路径不存在或文件存在时-代码不会停止,但会传递给第三个信号并导致窗口关闭/关闭。

如何处理错误/停止执行脚本而不关闭对话框窗口或不传递给其他按钮信号?基本上,在第一个或第二个信号之后停止而不关闭整个窗口。

1 个答案:

答案 0 :(得分:0)

让我们说前两个def或信号足以满足条件,这真的可以工作吗?从def checkfilexist调用信号,然后让下一个信号调用下一个信号,或者只是给出错误消息。

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