简体   繁体   English

python:使用 PyCharm 和 PyQt5 时,进程以退出代码 1 结束

[英]python: Process finished with exit code 1 when using PyCharm and PyQt5

I have three Python(3.4.3) scripts.我有三个 Python(3.4.3) 脚本。 One of them is for controlling the.ui file generated by PyQt5.其中之一是用于控制 PyQt5 生成的 .ui 文件。 When I run the GUI program it accepts all the data and everything and when I press the OK button on an InputDialog the window closes and the console displays.当我运行 GUI 程序时,它接受所有数据和所有内容,当我按下 InputDialog 上的 OK 按钮时,窗口关闭并显示控制台。

Process finished with exit code 1

When I run the same code on Python IDLE, it shows:当我在 Python IDLE 上运行相同的代码时,它显示:

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

This never happenned when I used this same Python(3.4.3 or 2.7) code on Visual Studio.当我在 Visual Studio 上使用相同的 Python(3.4.3 或 2.7)代码时,这从未发生过。 What could be the reason?可能是什么原因?

Here is the code of the python file controlling the.ui file.这是控制 .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_())

I have dealt with the same problem, and the answer is twofold:我处理过同样的问题,答案是双重的:

  1. The reason it's crashing could be any number of things.它崩溃的原因可能有很多。 It's probably a programming bug, calling a function that doesn't exist, passing a widget instead of a layout, etc. But since you're not getting useful output you don't know where to look for the culprit.这可能是一个编程错误,调用了一个不存在的函数,传递了一个小部件而不是布局,等等。但是由于您没有获得有用的输出,您不知道在哪里寻找罪魁祸首。 This is caused by:这是由以下原因引起的:
  2. PyQT raises and catches exceptions, but doesn't pass them along. PyQT 引发并捕获异常,但不传递它们。 Instead it just exits with a status of 1 to show an exception was caught.相反,它只是以状态 1 退出以显示异常已被捕获。

To catch the exceptions, you need to overwrite the sys exception handler:要捕获异常,您需要覆盖 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

Then in your execution code, wrap it in a try/catch.然后在您的执行代码中,将其包装在 try/catch 中。

try:
    sys.exit(app.exec_())
except:
    print("Exiting")

I had the same problem in pycharm, python 3.8, qt5.我在pycharm、python 3.8、qt5中遇到了同样的问题。 The stacktrace was never shown for qt errors inside pycharm; pycharm 中的 qt 错误从未显示堆栈跟踪; running the file from cmd the error was shown correctly instead.从 cmd 运行文件时错误显示正确。

I solved by doing the following: open Edit Configurations of the file you want to run, scroll down and check the box Emulate terminal in output console .我通过执行以下操作解决了问题:打开要运行的文件的编辑配置,向下滚动并选中Emulate terminal in output console框。

You have used self.sender.setText(email)您使用过self.sender.setText(email)

This is probably causing the problem in my opinion because, "sender" is the name in QObject's function and it does not have any setText attribute, so there may be the problem.在我看来,这可能是导致问题的原因,因为“发件人”是 QObject 函数中的名称,它没有任何 setText 属性,因此可能存在问题。

You have to specifically call the widget and setText to it.您必须专门调用小部件并为其设置文本。 For this, you can use instances of the py file of the layout creator.为此,您可以使用布局创建器的 py 文件实例。

I had the same issue when I was trying to use this self.ui.lineEdit().text() Here, the problem was -> I was calling the lineEdit function, whereas I had to use it's one attribute.我在尝试使用此self.ui.lineEdit().text()时遇到了同样的问题在这里,问题是 -> 我正在调用 lineEdit 函数,而我不得不使用它的一个属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM