在 Spyder 中运行 PyQt5 应用程序时,它总是以 -1 退出 [英] When running PyQt5 app in Spyder it always exits with -1

查看:22
本文介绍了在 Spyder 中运行 PyQt5 应用程序时,它总是以 -1 退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用 PyQt5 编程.没有问题,但有一个烦人的问题:当我启动应用程序时,我立即收到发生异常的消息并且 SystemExit: -1 被报告(见下文).

I am learning to program with PyQt5. No troubles with that, but there is one annoying issue: when I start the app, I immediately get the message that an exception occurred and SystemExit: -1 is repported (see below).

An exception has occurred, use %tb to see the full traceback.

SystemExit: -1

/home/arnold/bin/anaconda3/envs/ml-gpu/lib/python3.5/site-packages/IPython/core/interactiveshell.py:2918: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)


In [2]: %tb
Traceback (most recent call last):

  File "<ipython-input-1-f5ccc42a06e6>", line 1, in <module>
    runfile('/media/d/home/arnold/development/python/course_Qt/01-basic-window.py', wdir='/media/d/home/arnold/development/python/course_Qt')

  File "/home/arnold/bin/anaconda3/envs/ml-gpu/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 692, in runfile
    execfile(filename, namespace)

  File "/home/arnold/bin/anaconda3/envs/ml-gpu/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/media/d/home/arnold/development/python/course_Qt/01-basic-window.py", line 19, in <module>
    sys.exit(app.exec_()) # mind the underscore, without is python function

SystemExit: -1

程序很简单:

import sys
from PyQt5.QtWidgets import QApplication, QWidget

app = QApplication(sys.argv)
win = QWidget() # No arguments = toplevel window
win.setWindowTitle('Qt5 Window')
win.resize(640, 480)
win.show()

# wrap Qt5 call in sys.exit in order to catch any unhandled exception
# just good practice
sys.exit(app.exec_()) # mind the underscore, without is python function

之后程序运行良好,我可以做我想做的所有事情并正常退出.当我再次尝试运行该程序时,我收到以下消息:

The program runs fine after that, I can do all things what I want and normally exit. When I try to run the program again I get the message:

QCoreApplication::exec: The event loop is already running

我强行终止了程序,当我再次尝试运行它时,它又重新开始了.是我的设置还是有其他问题?谷歌搜索此错误时找不到提示.

I forcefully terminate the program and when I try to run it again it starts all over again. Is it my setup or is something else wrong? I could not find a hint when googling for this error.

设置:linux min 18.3、Spyder 3.5、conda 4.4.11、PyQt 5.10.1

Setup: linux min 18.3, Spyder 3.5, conda 4.4.11, PyQt 5.10.1

更新

下面卡洛斯的答案是正确的.我没有提到的是我将 QtDesigner 与 PyQt5 一起使用,但找不到合适的示例.这是我能找到的最好的将其调整为下面提到的解决方案 Carlos.保存 pyqt_first.pytax_calc.ui,运行 pyuic5 -o tax_calc.py tax_calc.ui 一切都应该运行良好.不要忘记将图形后端设置为 inline(工具 > 首选项 > IPython 控制台).

The answer of Carlos below is the correct one. What I did not mention is that I used QtDesigner with PyQt5 and couldn't find a decent example for it. This is the best I could find and I adjusted it to the solution Carlos mentioned below. Save pyqt_first.py and tax_calc.ui, run pyuic5 -o tax_calc.py tax_calc.ui and everything should run fine. Do not forget to set the graphics backend to inline(Tools > Preferences > IPython console).

#--- pyqt_first.py ---
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow
from tax_calc import Ui_MainWindow

class HelloWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.calc_tax_button.clicked.connect(self.CalculateTax)

    def CalculateTax(self):
        price = int(self.price_box.toPlainText())
        tax = (self.tax_rate.value())
        total_price = price  + ((tax / 100) * price)
        total_price_string = "The total price with tax is: " + str(total_price)
        self.results_window.setText(total_price_string)

if __name__ == "__main__":
    def run_app():
        app = QtWidgets.QApplication(sys.argv)
        mainWin = HelloWindow()
        mainWin.show()
        app.exec_()

    run_app()

#--- tax_calc.ui ---
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QTextEdit" name="price_box">
    <property name="geometry">
     <rect>
      <x>220</x>
      <y>100</y>
      <width>104</width>
      <height>71</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>120</y>
      <width>91</width>
      <height>31</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>10</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>Price</string>
    </property>
   </widget>
   <widget class="QSpinBox" name="tax_rate">
    <property name="geometry">
     <rect>
      <x>230</x>
      <y>250</y>
      <width>42</width>
      <height>22</height>
     </rect>
    </property>
    <property name="value">
     <number>20</number>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>250</y>
      <width>47</width>
      <height>13</height>
     </rect>
    </property>
    <property name="text">
     <string>Tax Rate</string>
    </property>
   </widget>
   <widget class="QPushButton" name="calc_tax_button">
    <property name="geometry">
     <rect>
      <x>230</x>
      <y>350</y>
      <width>111</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Calculate Tax</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="results_window">
    <property name="geometry">
     <rect>
      <x>180</x>
      <y>410</y>
      <width>131</width>
      <height>71</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>20</y>
      <width>441</width>
      <height>41</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>20</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>Sales Tax Calculator</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

推荐答案

(这里是 Spyder 维护者) 当你在 Spyder 中运行程序时,你是在 interactive 模式下运行的,这意味着您不需要在它们的末尾添加 sys.exit 调用,因为这只是试图杀死负责运行您的代码的进程.

(Spyder maintainer here) When you run programs in Spyder you're doing it in interactive mode, meaning that you don't need to add a sys.exit call at the end of them, because that simply tries to kill the process responsible for running your code.

这正是这个警告告诉你的:

That's precisely what this warning is telling you:

warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

这意味着要退出 IPython 会话,您需要遵循上述方法之一,而不是使用 sys.exit.

It means that to exit an IPython session you need to follow one of the above methods, instead of using sys.exit.

这个问题的解决方案(已经被问过很多次了)在我们的 wiki.

The solution to this problem (which has been asked a lot of times already) is in our wiki.

这篇关于在 Spyder 中运行 PyQt5 应用程序时,它总是以 -1 退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆