2015-08-14 73 views
12

我正在構建一個將matplotlib圖嵌入到GUI中的應用程序。問題是,一旦我從matplotlib添加了任何東西到我的代碼中(除了導入,那些工作像往常一樣),我的應用就崩潰了。問題發生在我的課Solver_Apptk.Tk.__init__(self, *args, **kwargs)。當我運行代碼時,出現大量錯誤,應用程序崩潰。下面是我的一些代碼:Matplotlib崩潰tkinter應用程序

import tkinter as tk 
from tkinter import ttk 

import matplotlib 
from matplotlib import pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg 
from matplotlib.figure import Figure 

# Setting up figures for integration in GUI: 
fig_3D = plt.figure() 
fig_2D = plt.figure() 
a_3D = fig_3D.add_subplot(111, projection="3d") 
a_2D = fig_2D.add_subplot(111) 
a_3D.plot_wireframe([1, 2, 3, 4, 5], [1, 3, 7, 6, 4], [1, 2, 3, 4, 5], color="blue") 

class Solver_App(tk.Tk, ttk.Frame): 

    def __init__(self, *args, **kwargs): 

     tk.Tk.__init__(self, *args, **kwargs) # This is where the app crashes. 

     # Equation frame holds everything related to the input and configuration of the equations. 
     equation_frame = ttk.Frame(self) 
     equation_frame.pack(side="bottom", fill="x", pady=50, padx=50) 

     # More code goes here... 

# There are more classes with a similar setup as the one above... 

app = Solver_App() 
app.mainloop() 

這裏是巨大的錯誤運行代碼時,我得到:

2015-08-14 15:18:29.142 Python[50796:18837594] -[NSApplication _setup:]: unrecognized selector sent to instance 0x10216a830 
2015-08-14 15:18:29.143 Python[50796:18837594] An uncaught exception was raised 
2015-08-14 15:18:29.143 Python[50796:18837594] -[NSApplication _setup:]: unrecognized selector sent to instance 0x10216a830 
2015-08-14 15:18:29.144 Python[50796:18837594] (
    0 CoreFoundation      0x00007fff9901b03c __exceptionPreprocess + 172 
    1 libobjc.A.dylib      0x00007fff9436476e objc_exception_throw + 43 
    2 CoreFoundation      0x00007fff9901e0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
    3 CoreFoundation      0x00007fff98f63e24 ___forwarding___ + 1028 
    4 CoreFoundation      0x00007fff98f63998 _CF_forwarding_prep_0 + 120 
    5 Tk         0x00000001024ad527 TkpInit + 476 
    6 Tk         0x0000000102427aca Tk_Init + 1788 
    7 _tkinter.so       0x00000001006e5f2d Tcl_AppInit + 77 
    8 _tkinter.so       0x00000001006e30d6 Tkinter_Create + 934 
    9 Python        0x00000001000e44ce PyEval_EvalFrameEx + 28894 
    10 Python        0x00000001000e5ced PyEval_EvalCodeEx + 2349 
    11 Python        0x000000010003e8ba function_call + 186 
    12 Python        0x000000010000d3c8 PyObject_Call + 104 
    13 Python        0x00000001000e0cb9 PyEval_EvalFrameEx + 14537 
    14 Python        0x00000001000e5ced PyEval_EvalCodeEx + 2349 
    15 Python        0x000000010003e8ba function_call + 186 
    16 Python        0x000000010000d3c8 PyObject_Call + 104 
    17 Python        0x000000010002802c method_call + 140 
    18 Python        0x000000010000d3c8 PyObject_Call + 104 
    19 Python        0x000000010007b831 slot_tp_init + 81 
    20 Python        0x0000000100072d14 type_call + 212 
    21 Python        0x000000010000d3c8 PyObject_Call + 104 
    22 Python        0x00000001000e1b09 PyEval_EvalFrameEx + 18201 
    23 Python        0x00000001000e5ced PyEval_EvalCodeEx + 2349 
    24 Python        0x00000001000e5daf PyEval_EvalCode + 63 
    25 Python        0x000000010011048e PyRun_FileExFlags + 206 
    26 Python        0x000000010011083d PyRun_SimpleFileExFlags + 717 
    27 Python        0x000000010012810e Py_Main + 3262 
    28 Python        0x0000000100000e32 Python + 3634 
    29 Python        0x0000000100000c84 Python + 3204 
    30 ???         0x0000000000000002 0x0 + 2 
) 
2015-08-14 15:18:29.144 Python[50796:18837594] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x10216a830' 
*** First throw call stack: 
(
    0 CoreFoundation      0x00007fff9901b03c __exceptionPreprocess + 172 
    1 libobjc.A.dylib      0x00007fff9436476e objc_exception_throw + 43 
    2 CoreFoundation      0x00007fff9901e0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
    3 CoreFoundation      0x00007fff98f63e24 ___forwarding___ + 1028 
    4 CoreFoundation      0x00007fff98f63998 _CF_forwarding_prep_0 + 120 
    5 Tk         0x00000001024ad527 TkpInit + 476 
    6 Tk         0x0000000102427aca Tk_Init + 1788 
    7 _tkinter.so       0x00000001006e5f2d Tcl_AppInit + 77 
    8 _tkinter.so       0x00000001006e30d6 Tkinter_Create + 934 
    9 Python        0x00000001000e44ce PyEval_EvalFrameEx + 28894 
    10 Python        0x00000001000e5ced PyEval_EvalCodeEx + 2349 
    11 Python        0x000000010003e8ba function_call + 186 
    12 Python        0x000000010000d3c8 PyObject_Call + 104 
    13 Python        0x00000001000e0cb9 PyEval_EvalFrameEx + 14537 
    14 Python        0x00000001000e5ced PyEval_EvalCodeEx + 2349 
    15 Python        0x000000010003e8ba function_call + 186 
    16 Python        0x000000010000d3c8 PyObject_Call + 104 
    17 Python        0x000000010002802c method_call + 140 
    18 Python        0x000000010000d3c8 PyObject_Call + 104 
    19 Python        0x000000010007b831 slot_tp_init + 81 
    20 Python        0x0000000100072d14 type_call + 212 
    21 Python        0x000000010000d3c8 PyObject_Call + 104 
    22 Python        0x00000001000e1b09 PyEval_EvalFrameEx + 18201 
    23 Python        0x00000001000e5ced PyEval_EvalCodeEx + 2349 
    24 Python        0x00000001000e5daf PyEval_EvalCode + 63 
    25 Python        0x000000010011048e PyRun_FileExFlags + 206 
    26 Python        0x000000010011083d PyRun_SimpleFileExFlags + 717 
    27 Python        0x000000010012810e Py_Main + 3262 
    28 Python        0x0000000100000e32 Python + 3634 
    29 Python        0x0000000100000c84 Python + 3204 
    30 ???         0x0000000000000002 0x0 + 2 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

我已經從一個教程[這裏找到]引用代碼(http://pythonprogramming.net/how-to-embed-matplotlib-graph-tkinter-gui/「如何嵌入一​​個Matplotlib圖形到你的Tkinter GUI「),但他們的例子似乎工作正常。造成這個問題的原因是什麼?如何解決?

  • 我使用Python 3.4,matplotlib 1.4.3,並運行Mac OS Yosemite。
+0

我覺得你已經省略了最重要的部分。看看你是否可以把它提煉成一個[MCVE](http://stackoverflow.com/help/mcve),它可能會幫助你解決你的問題。一個技巧,當你這樣做:當在Tkinter中嵌入東西時,不要使用'pyplot';使用你輸入的實際的'圖'。如果你正在做或不做,你跳過了我們可以看到的地方。 – Ajean

+0

請嘗試執行以下操作。 'plt.rcParams ['keymap.save'] ='''這可能是mac擁有鍵綁定的結果。上次我在Mac上由tkinter引發了一個NSException,這是由於tkinter和mac與它們的鍵綁定相沖突的結果。 – Pythonista

回答

27

您需要明確設置TkAgg後端。我可以重現你的錯誤。使用下面的代碼,問題就解決了。

import matplotlib 
matplotlib.use("TkAgg") 
from matplotlib import pyplot as plt 

注意,設置TkAgg後端進口pyplot無法正常工作或之後;它也崩潰了。您需要在導入pyplot之前設置它。 (經過MPL 1.4.3,tkinter.TkVersion 8.6測試)

+0

固定對我來說 - 測試與mpl 1.5.1,tkinter.TkVersion 8.5 – theicfire

+0

就像一個魅力!謝謝。 – Rynkadink