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

2024-04-28 22:59:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个对话框窗口,里面有各种各样的linedit和按钮,它们调用各种函数。 一个按钮发送各种信号,当发生异常/错误时,它关闭整个窗口。 我发现了一些类似的情况,但这一个是具体的,有各种信号

现在这是我要检查行编辑中的路径和文件是否存在的代码。如果不是,我想显示消息并保持对话框窗口打开。因此,在不执行进一步信号和关闭窗口的情况下处理错误^{<不幸的是,关闭了整个窗口

到目前为止,我的情况是:

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

当路径不存在或文件存在时-代码不会停止,而是传递到第三个信号并失败/关闭窗口

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


Tags: pathimportself信号osdefsysdialog
1条回答
网友
1楼 · 发布于 2024-04-28 22:59:38

假设前两个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

相关问题 更多 >