【问题标题】:.show() and/or.exec() from Pyqt5 not working in SpyderPyqt5 中的 .show() 和/或 .exec() 在 Spyder 中不起作用
【发布时间】:2018-05-26 18:05:58
【问题描述】:

我昨天在新安装了 Windows 10 的计算机上安装了 Python 3.6.3 和 Spyder 3.2.4,它们都与 Pyqt5 一起安装。我在 Spyder 中运行一些以前编写的 Pyqt5 代码时遇到问题。下面的代码显示了重现问题的最小代码 sn-p。

Spyder IPython 控制台在运行代码时无限期挂起,并且从不打开窗口。 Ctrl-C 不会停止执行,所以我必须杀死 Spyder。如果我尝试逐行运行它,我观察到“w.show()”执行但什么也不做,而“app.exec()”是挂起的行。相反,如果我从命令行控制台运行它,“w.show()”会显示一个非功能性窗口,而“app.exec()”会使该窗口正常工作。我在 Spyder 中运行的所有非 pyqt 代码都执行得很好。

我更喜欢在 Spyder 中开发代码,因为从命令行运行代码通常需要 30 到 60 秒才能开始(可能来自导入。)

我可以在我的旧(现已损坏)系统上运行 Spyder 中的代码,所以很明显这是我的安装或计算机的问题。但我不知道是什么。我已经尝试禁用我的病毒软件,使用 pip 更新所有相关软件包,从 Anaconda 提示符重置 Spyder,然后重新启动计算机。还有其他想法吗?

import sys
from PyQt5.QtWidgets import QApplication, QWidget

if not QApplication.instance():
   app = QApplication(sys.argv)
else:
   app = QApplication.instance()
print("app started")
w = QWidget()
w.resize(250, 250)
w.move(200, 200)
w.setWindowTitle('Test window')
print('app setting')
w.show()
print("shown")
app.exec_()
#sys.exit(app.exec_())
print("exit")

【问题讨论】:

  • 请阅读github.com/spyder-ide/spyder/wiki/… 了解为什么会发生这种情况。
  • 不,这不是问题所在。正如您在第一行中所说,这是“多次运行 QApplication 时的问题”。我遇到的问题是第一次发生,它杀死了 Spyder,所以只有第一次。我将 sn-p 添加到我的代码中,但它并没有解决问题。
  • 您在运行代码之前是否激活了 Qt5 事件循环?
  • 是的,我更新了代码,如原始帖子的编辑中所示。它仍然会杀死 Spyder。
  • 我认为您缺少 app = QApplication.instance() 以使您的代码正常工作。我真的不知道这是否是问题所在,但是在使用最新的 Spyder 版本 (3.2.4) 时,在 Linux 或 Windows 上运行您的代码后,我没有遇到内核崩溃。

标签: python-3.x pyqt5 spyder


【解决方案1】:

在 python 中运行时,需要进行如下修改。我已经稍微修改了您的脚本,以便能够让它为您运行一些主要的 PyQt 约定。另请参阅我的内联 cmets,以了解您尝试使用打印行完成的工作。尽情享受吧!

# -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget

class MyMainUI(QtWidgets.QMainWindow):

    def __init__(self, parent=None):

        print ' Start Main'

        super(MyMainUI, self).__init__(parent)
        self.setWindowTitle('Test window')
        self.resize(250, 250)
        self.move(200, 200)
        MySpidercodeHook()

    def MySpiderCodeHook(self):
        ...link/run code...

if __name__ == '__main__':

    app = QApplication(sys.argv)      # no QApplication.instance() > returns -1 or error. 
                                      # Your setting up something that cannot yet be peeked
                                      # into at this stage.

    print("app started")
    w = MyMainUI()                    # address your main GUI application
                                      # not solely a widgetname..that is confusing...

    x = 200
    y = 200
#    w.move(x, y)                     # normally this setting is done within the mainUI. 
                                      # See also PyQT conventions.
    print 'app setting: moving window to position : %s' % ([x, y])
    w.show()
    print("shown")
    app.exec_()
    #sys.exit(app.exec_())  # if active then print "exit" is not shown.. 
                            # now is while using app.exec_()
    #sys.exit(-1)       # will returns you '-1' value as handle. Can be any integer.
    print("exit")
    sys.stdout.flush()  # line used to print "printing text" when you're in an IDE 
                        # like Komodo EDIT.

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2015-09-12
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    相关资源
    最近更新 更多