简体   繁体   English

使用qt4agg运行matplotlib后关闭时,Python脚本由于导入而崩溃

[英]Python script crashes when closing after running matplotlib with qt4agg because of import

I have written a script using matplotlib, which runs just fine with the standard matplotlib. 我已经使用matplotlib编写了一个脚本,该脚本可以在标准matplotlib上正常运行。 The script is written with the plot as a class, and calling Plot() is enough to get it running. 该脚本是将绘图作为类编写的,并且调用Plot()足以使其运行。

Now I want to add some buttons to the toolbar, and to do this I am using qt4agg because I have installed matplotlib via Anaconda. 现在,我想向工具栏添加一些按钮,并使用qt4agg进行此操作,因为我已经通过Anaconda安装了matplotlib。 When writing the code for the main window I used this example, and it runs just fine. 在为主窗口编写代码时,我使用了这个示例,它运行得很好。 In order to use the plot script I have already written I want to pass the figure created in the QT-script to the Plot()-class. 为了使用我已经编写的绘图脚本,我想将在QT脚本中创建的图形传递给Plot()类。

This solution works just fine, until I try to close the window. 在我尝试关闭窗口之前,此解决方案效果很好。 The window closes, and python crashes. 窗口关闭,python崩溃。 It crashes even though I do not call the Plot()-class, and the only way to get it to not crash is to remove the line importing the file. 即使我不调用Plot()类,它也会崩溃,并且使其不崩溃的唯一方法是删除导入文件的行。 Is there something special I need to think about when importing a script into a window? 将脚本导入窗口时,我需要考虑一些特别的东西吗?

from __future__ import print_function
import sys
from matplotlib.figure import Figure
from matplotlib.backend_bases import key_press_handler
### LINE CAUSING TROUBLE
from plotting import Plot
###
from test import *
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.backends import qt_compat
use_pyside = qt_compat.QT_API == qt_compat.QT_API_PYSIDE

if use_pyside:
    print ("USING PYSIDE")
    from PySide.QtCore import *
    from PySide.QtGui import *
else:
    print("NOT USING PYSIDE")
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *

class AppForm(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.create_main_frame()
        self.on_draw()

    def create_main_frame(self):
        self.main_frame = QWidget()

        self.fig = Figure()
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setParent(self.main_frame)
        self.canvas.setFocusPolicy(Qt.StrongFocus)
        self.canvas.setFocus()

        self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)

        self.canvas.mpl_connect('key_press_event', self.on_key_press)

        vbox = QVBoxLayout()
        vbox.addWidget(self.canvas)
        vbox.addWidget(self.mpl_toolbar)
        self.main_frame.setLayout(vbox)
        self.setCentralWidget(self.main_frame)

    def on_draw(self):
        self.fig.clear()
        #Plot(self.fig)
        self.canvas.draw()

    def on_key_press(self, event):
        key_press_handler(event, self.canvas, self.mpl_toolbar)

def main():
    app = QApplication(sys.argv)
    form = AppForm()
    form.show()
    app.exec_()

if __name__ == "__main__":
    main()

Here is a very compressed version of the other file that still causes the error. 这是另一个文件的压缩版本,仍然会导致错误。

import matplotlib.pyplot as pplt

class Plot():
    def __init__(self, figure=pplt.figure()):
        self.figure = figure

i had the same problems with Anaconda and Python(x,y) . 我对AnacondaPython(x,y)有同样的问题。 then i tried to install Python from scratch : 然后我试图从头开始安装Python

  1. python-2.7.10.msi python-2.7.10.msi
  2. PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x32.exe PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x32.exe
  3. VCForPython27.msi VCForPython27.msi
  4. pip install matplotlib pip安装matplotlib

it doesn't solve all crashes. 它不能解决所有崩溃问题。 you could also try this : 您也可以尝试以下方法:

self.setAttribute(Qt.WA_DeleteOnClose)

for example : 例如 :

class AppForm(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setAttribute(Qt.WA_DeleteOnClose) # <---
        self.create_main_frame()
        self.on_draw()
...

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

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