![](/img/trans.png)
[英]How to stop running PyQt5 program without closing the GUI window?
[英]PyQt - how to stop execution without closing the dialog window
我有一個對話框窗口,里面有各種 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_())
當路徑不存在或文件存在時 - 代碼不會停止,而是傳遞到第三個信號並失敗/關閉窗口。
如何在不關閉對話框窗口或不傳遞給其他按鈕信號的情況下處理錯誤/停止執行腳本? 基本上,在第一個或第二個信號后停止而不關閉整個窗口。
假設前兩個定義或信號足以滿足條件,這真的可以工作嗎? 從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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.