tkagg(python)DLL加载失败:%1不是有效的Win32应用程序

时间:2016-04-11 15:37:25

标签: python-3.x matplotlib tkinter tkinter-canvas

我正在用tkinter和python 3.5编写一个应用程序。我正在使用64位的Windows 7。然而,看起来“导入matplotlib.backends.tkagg作为tkagg”导致了麻烦。我试图在anaconda环境中工作,也不在那个环境中工作。愿有人帮帮我吗?

这就是错误:

Traceback (most recent call last):
  File "C:\Users\michele.dellamea\Desktop\matplot\tkinterrr.py", line 4, in <module>
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
  File "C:\Users\michele.dellamea\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 13, in <module>
    import matplotlib.backends.tkagg as tkagg
  File "C:\Users\michele.dellamea\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\backends\tkagg.py", line 9, in <module>
    from matplotlib.backends import _tkagg
ImportError: DLL load failed: %1 is not a valid Win32 application.
[Finished in 1.3s with exit code 1]
[shell_cmd: python -u "C:\Users\michele.dellamea\Desktop\matplot\tkinterrr.py"]
[dir: C:\Users\michele.dellamea\Desktop\matplot]
[path: C:\ProgramData\Oracle\Java\javapath;c:\Program Files (x86)\AMD APP\bin\x86_64;c:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Skype\Phone\;C:\Users\michele.dellamea\AppData\Local\Programs\Python\Python35-32\;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\michele.dellamea\AppData\Local\Continuum\Anaconda3;C:\Users\michele.dellamea\AppData\Local\Continuum\Anaconda3\Scripts;C:\Users\michele.dellamea\AppData\Local\Continuum\Anaconda3\Library\bin]

这就是代码:

import matplotlib
matplotlib.use("TkAgg")

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
#import matplotlib.pylab as pylab

import tkinter as tk
from tkinter import ttk #css for tkinter



LARGE_FONT = ("Verdana", 12)


class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs): #args all var, kwargs all dict

        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.iconbitmap(self, default="agnul.jpg")
        tk.Tk.wm_title(self, "Sea of BTC client")

        container = tk.Frame(self) #frame hedge window
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, PageOne, PageTwo, PageThree):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew") #you specify all grid

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont] # key
        frame.tkraise()

def qf(stringtoprint):
    print(stringtoprint)

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Start Page", font=LARGE_FONT) #Label class label object
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Visit Page 1", command=lambda: controller.show_frame(PageOne))
        button1.pack()

        button2 = ttk.Button(self, text="Visit Page 2", command=lambda: controller.show_frame(PageTwo))
        button2.pack()

        button3 = ttk.Button(self, text="Graph Page", command=lambda: controller.show_frame(PageThree))
        button3.pack()

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page 1", font=LARGE_FONT) #Label class label object
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Back to Home", command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = ttk.Button(self, text="Page 2", command=lambda: controller.show_frame(PageTwo))
        button2.pack()

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page 2", font=LARGE_FONT) #Label class label object
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Back to Home", command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = ttk.Button(self, text="Page 1", command=lambda: controller.show_frame(PageOne))
        button2.pack()

class PageThree(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Graph Page", font=LARGE_FONT) #Label class label object
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Back to Home", command=lambda: controller.show_frame(StartPage))
        button1.pack()

        f = Figure(figsize=(5,5), dpi=100)
        a = f.add_subplot(111)
        a.plot([1,2,3,4,5,6,7,8],[5,6,1,3,8,9,3,5])

        canvas = FigureCanvasTkAgg(f, self)
        canvas.show()
        canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)

        toolbar = NavigationToolbar2TkAgg(canvas, self)
        toolbar.update()
        canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)


app = SeaofBTCapp()
app.mainloop()

1 个答案:

答案 0 :(得分:0)

使用dependency walker打开 C:\ Program Files \ Python 3 \ Lib \ site-packages \ matplotlib \ backends \ backend_tkagg.pyd 文件,找到丢失的dll及其位置是预期的。

我遇到同样的问题,后端查找了 pythonFolder \ Lib \ site-packages \ matplotlib \ backend 文件夹中缺少的 msvcp140.dll 文件,但实际存在在 pythonFolder \ Lib \ site-packages \ matplotlib 文件夹中。所以我将它复制到正确的文件夹中以使其正常工作。