我是Python的绝对新手。
我想在应用程序启动之前检查文件是否存在。 如果文件不存在,我认为应该有一个错误消息框,其中包含一个关闭应用程序的按钮。
这是我的代码
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMessageBox
from decryptwindow import Ui_Dialog # importing our generated file
import sys
import os
class mywindow(QtWidgets.QMainWindow):
def __init__(self):
super(mywindow, self).__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
ini_file = 'config.ini'
ini_file_exists = os.path.isfile(ini_file)
if ini_file_exists:
# just for testing
print(ini_file + ' is found')
# let`s show the main window
app = QtWidgets.QApplication([])
application = mywindow()
application.show()
sys.exit(app.exec())
else:
# just for testing
print(ini_file + ' is not found')
app = QtWidgets.QApplication([])
application = mywindow()
application.show()
error_dialog = QtWidgets.QErrorMessage()
error_dialog.showMessage(ini_file + ' is not found')
# after message I would like just close the application
sys.exit(app.exec())
答案 0 :(得分:0)
那很简单。 真可惜,我在发布之前没有尝试过。
删除这两行可以解决问题
application = mywindow()
application.show()