python:使用 PyCharm 和 PyQt5 时进程已完成,退出代码 1

2023-11-24

我有三个 Python(3.4.3) 脚本。其中之一是用于控制 PyQt5 生成的 .ui 文件。当我运行 GUI 程序时,它接受所有数据和所有内容,当我按 InputDialog 上的“确定”按钮时,窗口将关闭并显示控制台。

Process finished with exit code 1

当我在 Python IDLE 上运行相同的代码时,它显示:

<<<<<<RESTART>>>>>>

当我在 Visual Studio 上使用相同的 Python(3.4.3 或 2.7)代码时,这种情况从未发生过。可能是什么原因?

这是控制 .ui 文件的 python 文件的代码。

import sys
from PyQt5 import QtCore, QtGui, uic, QtWidgets
from Email import encrypt_email
from Email import decrypt_email
from Email import newuser

qtCreatorFile = "rsegui.ui" # Enter file here.

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        user, ok = QtWidgets.QInputDialog.getText(self, 'New User', 
    'Are you a new user?')
        user=str(user)
        if user in "YESYesyesYy":
            email, ok = QtWidgets.QInputDialog.getText(self, 'New User', 
    'Enter Your Email ID:')
            email1=str(email)
            self.sender.setText(email)
            newuser(email1)

    self.encrypt_and_send.clicked.connect(self.EncryptEmail)
    self.decrypt.clicked.connect(self.DecryptEmail)
    self.clear.clicked.connect(self.ClearEncrypt)
    self.clear_2.clicked.connect(self.ClearDecrypt)
    self.sender.setPlaceholderText("Your Email ID")
    self.receiver.setPlaceholderText("Receivers, Separate them by ';'")
    self.subject.setPlaceholderText("Enter Subject")
    self.message.setPlaceholderText("Enter Message")
    self.sender_2.setPlaceholderText("Your Email ID")
    self.message_2.setPlaceholderText("Encrypted Text")



    def EncryptEmail(self):
       sender = str(self.sender.text())
       receiver = str(self.receiver.text())
       receivers = receiver.split(';')
       subject = str(self.subject.text())
       message = str(self.message.text())
       password, ok = QtWidgets.QInputDialog.getText(self, 'Password', 
'Enter your password:',QtWidgets.QLineEdit.Password)
       encrypt_email(sender,receivers,subject,message,password)

    def DecryptEmail(self):
       email = str(self.sender_2.text())
       message = str(self.message_2.text())
       self.decrypted.setText(decrypt_email(email,message))

    def ClearDecrypt(self):
       self.sender_2.clear()
       self.message_2.clear()

    def ClearEncrypt(self):
       self.sender.clear()
       self.message.clear()
       self.receiver.clear()
       self.subject.clear()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

我处理过同样的问题,答案有两个:

  1. 它崩溃的原因可能有很多。这可能是一个编程错误,调用一个不存在的函数,传递一个小部件而不是布局等。但是由于您没有获得有用的输出,您不知道在哪里寻找罪魁祸首。这是由于:
  2. PyQT 引发并捕获异常,但不会传递它们。相反,它只是以状态 1 退出,表明捕获了异常。

要捕获异常,您需要覆盖 sys 异常处理程序:

# Back up the reference to the exceptionhook
sys._excepthook = sys.excepthook

def my_exception_hook(exctype, value, traceback):
    # Print the error and traceback
    print(exctype, value, traceback)
    # Call the normal Exception hook after
    sys._excepthook(exctype, value, traceback)
    sys.exit(1)

# Set the exception hook to our wrapping function
sys.excepthook = my_exception_hook

然后在执行代码中,将其包装在 try/catch 中。

try:
    sys.exit(app.exec_())
except:
    print("Exiting")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python:使用 PyCharm 和 PyQt5 时进程已完成,退出代码 1 的相关文章

随机推荐