qt - Python script crashes when closing after running matplotlib with qt4agg because of import -
i have written script using matplotlib, runs fine standard matplotlib. script written plot class, , calling plot() enough running.
now want add buttons toolbar, , using qt4agg because have installed matplotlib via anaconda. when writing code main window used this example, , runs fine. in order use plot script have written want pass figure created in qt-script plot()-class.
this solution works fine, until try close window. window closes, , python crashes. crashes though not call plot()-class, , way not crash remove line importing file. there special need think when importing script window?
from __future__ import print_function import sys matplotlib.figure import figure matplotlib.backend_bases import key_press_handler ### line causing trouble plotting import plot ### test import * matplotlib.backends.backend_qt4agg import figurecanvasqtagg figurecanvas matplotlib.backends.backend_qt4agg import navigationtoolbar2qt navigationtoolbar matplotlib.backends import qt_compat use_pyside = qt_compat.qt_api == qt_compat.qt_api_pyside if use_pyside: print ("using pyside") pyside.qtcore import * pyside.qtgui import * else: print("not using pyside") pyqt4.qtcore import * 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 compressed version of other file still causes error.
import matplotlib.pyplot pplt class plot(): def __init__(self, figure=pplt.figure()): self.figure = figure
i had same problems anaconda , python(x,y). tried install python scratch :
- python-2.7.10.msi
- pyqt4-4.11.4-gpl-py2.7-qt4.8.7-x32.exe
- vcforpython27.msi
- pip install matplotlib
it doesn't solve crashes. try :
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() ...
Comments
Post a Comment