我只是学习PyQt,我有一个小应用程序似乎工作正常单位我点击对话框右上角的X关闭它。 当我这样做并返回控制台时,我发现异常已经引发如下:
To exit: use 'exit', 'quit', or Ctrl-D. An exception has occurred, use %tb to see the full traceback. SystemExit: 0 In [2]: %tb Traceback (most recent call last): File "<ipython-input-1-4524246fa84a>", line 1, in <module> runfile('C:/Users/21025/simpleAdder.pyw', wdir='C:/Users/21025') File "C:\Users\21035\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 586, in runfile execfile(filename, namespace) File "C:/Users/21035/simpleAdder.pyw", line 81, in <module> sys.exit(app.exec_()) SystemExit: 0我究竟做错了什么 ? 该应用程序的代码如下所示:
from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName(_fromUtf8("Dialog")) Dialog.resize(673, 565) self.v1Input = QtGui.QLineEdit(Dialog) self.v1Input.setGeometry(QtCore.QRect(50, 70, 71, 20)) self.v1Input.setObjectName(_fromUtf8("v1Input")) self.v2Input = QtGui.QLineEdit(Dialog) self.v2Input.setGeometry(QtCore.QRect(150, 70, 71, 20)) self.v2Input.setObjectName(_fromUtf8("v2Input")) self.v3Input = QtGui.QLineEdit(Dialog) self.v3Input.setGeometry(QtCore.QRect(250, 70, 71, 20)) self.v3Input.setObjectName(_fromUtf8("v3Input")) self.calc_result = QtGui.QLineEdit(Dialog) self.calc_result.setGeometry(QtCore.QRect(420, 70, 113, 20)) self.calc_result.setObjectName(_fromUtf8("calc_result")) self.label = QtGui.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(60, 50, 46, 13)) self.label.setObjectName(_fromUtf8("label")) self.label_2 = QtGui.QLabel(Dialog) self.label_2.setGeometry(QtCore.QRect(160, 50, 46, 13)) self.label_2.setObjectName(_fromUtf8("label_2")) self.label_3 = QtGui.QLabel(Dialog) self.label_3.setGeometry(QtCore.QRect(260, 50, 46, 13)) self.label_3.setObjectName(_fromUtf8("label_3")) self.label_4 = QtGui.QLabel(Dialog) self.label_4.setGeometry(QtCore.QRect(450, 50, 46, 13)) self.label_4.setObjectName(_fromUtf8("label_4")) self.pushButton = QtGui.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(200, 230, 75, 23)) self.pushButton.setObjectName(_fromUtf8("pushButton")) self.retranslateUi(Dialog) QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.v1Input.clear) QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.v2Input.clear) QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.v3Input.clear) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): Dialog.setWindowTitle(_translate("Dialog", "Dialog", None)) self.label.setText(_translate("Dialog", "Val 1", None)) self.label_2.setText(_translate("Dialog", "Val 2", None)) self.label_3.setText(_translate("Dialog", "Val 3", None)) self.label_4.setText(_translate("Dialog", "Result", None)) self.pushButton.setText(_translate("Dialog", "Clear Inputs", None)) if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) Dialog = QtGui.QDialog() ui = Ui_Dialog() ui.setupUi(Dialog) Dialog.show() sys.exit(app.exec_())I'm just learning PyQt and I have a small application that seems to work ok unit I hit the X in the upper right corner of the dialog box to close it down. When I do that and return to the console I see that an exception has been raised as follows:
To exit: use 'exit', 'quit', or Ctrl-D. An exception has occurred, use %tb to see the full traceback. SystemExit: 0 In [2]: %tb Traceback (most recent call last): File "<ipython-input-1-4524246fa84a>", line 1, in <module> runfile('C:/Users/21025/simpleAdder.pyw', wdir='C:/Users/21025') File "C:\Users\21035\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 586, in runfile execfile(filename, namespace) File "C:/Users/21035/simpleAdder.pyw", line 81, in <module> sys.exit(app.exec_()) SystemExit: 0What am I doing wrong ? Code for the application is shown below:
from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName(_fromUtf8("Dialog")) Dialog.resize(673, 565) self.v1Input = QtGui.QLineEdit(Dialog) self.v1Input.setGeometry(QtCore.QRect(50, 70, 71, 20)) self.v1Input.setObjectName(_fromUtf8("v1Input")) self.v2Input = QtGui.QLineEdit(Dialog) self.v2Input.setGeometry(QtCore.QRect(150, 70, 71, 20)) self.v2Input.setObjectName(_fromUtf8("v2Input")) self.v3Input = QtGui.QLineEdit(Dialog) self.v3Input.setGeometry(QtCore.QRect(250, 70, 71, 20)) self.v3Input.setObjectName(_fromUtf8("v3Input")) self.calc_result = QtGui.QLineEdit(Dialog) self.calc_result.setGeometry(QtCore.QRect(420, 70, 113, 20)) self.calc_result.setObjectName(_fromUtf8("calc_result")) self.label = QtGui.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(60, 50, 46, 13)) self.label.setObjectName(_fromUtf8("label")) self.label_2 = QtGui.QLabel(Dialog) self.label_2.setGeometry(QtCore.QRect(160, 50, 46, 13)) self.label_2.setObjectName(_fromUtf8("label_2")) self.label_3 = QtGui.QLabel(Dialog) self.label_3.setGeometry(QtCore.QRect(260, 50, 46, 13)) self.label_3.setObjectName(_fromUtf8("label_3")) self.label_4 = QtGui.QLabel(Dialog) self.label_4.setGeometry(QtCore.QRect(450, 50, 46, 13)) self.label_4.setObjectName(_fromUtf8("label_4")) self.pushButton = QtGui.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(200, 230, 75, 23)) self.pushButton.setObjectName(_fromUtf8("pushButton")) self.retranslateUi(Dialog) QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.v1Input.clear) QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.v2Input.clear) QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.v3Input.clear) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): Dialog.setWindowTitle(_translate("Dialog", "Dialog", None)) self.label.setText(_translate("Dialog", "Val 1", None)) self.label_2.setText(_translate("Dialog", "Val 2", None)) self.label_3.setText(_translate("Dialog", "Val 3", None)) self.label_4.setText(_translate("Dialog", "Result", None)) self.pushButton.setText(_translate("Dialog", "Clear Inputs", None)) if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) Dialog = QtGui.QDialog() ui = Ui_Dialog() ui.setupUi(Dialog) Dialog.show() sys.exit(app.exec_())最满意答案
如消息所示,代码包括sys.exit(app.exec_()) ,它将执行GUI,然后调用exit例程。 您是通过交互式提示调用此函数,因此它不会退出,而是通知您尝试从所调用的内容中退出。 如果您希望能够在没有错误的情况下从交互式提示中调用此代码,只需删除退出部分,将sys.exit(app.exec_())更改为sys.exit(app.exec_()) app.exec_() 。
As the message suggests, the code includes sys.exit(app.exec_()), which will execute the GUI, and then call the exit routine. You called this from an interactive prompt, and so instead of quitting, it informed you that you tried to quit from within the thing you called. If you want to be able to call this code from an interactive prompt without the error, simply remove the exit part, change sys.exit(app.exec_()) to app.exec_().
更多推荐
发布评论