cx_freeze与Tkinter& matplotlib后端,没有名为FileDialog的模块

时间:2016-01-24 17:57:13

标签: python python-2.7 matplotlib cx-freeze python-standalone

使用cx_Freeze构建简单的matplotlib应用程序非常有用,但是当我尝试从Tkinter&amp ;;创建一个独立的可执行文件时,我遇到了一个问题。 Matplotlib应用程序。

这是一个重现错误的最小例子:

#!/usr/bin/env python

import matplotlib
matplotlib.use('TkAgg')

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler


from matplotlib.figure import Figure

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

root = Tk.Tk()
root.wm_title("Embedding in TK")


f = Figure(figsize=(5, 4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)

a.plot(t, s)


# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)


def on_key_event(event):
    print('you pressed %s' % event.key)
    key_press_handler(event, canvas, toolbar)

canvas.mpl_connect('key_press_event', on_key_event)


def _quit():
    root.quit()     # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent
                    # Fatal Python Error: PyEval_RestoreThread: NULL tstate

button = Tk.Button(master=root, text='Quit', command=_quit)
button.pack(side=Tk.BOTTOM)

Tk.mainloop()
# If you put root.destroy() here, it will cause an error if
# the window is closed with the window manager.

特别是from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg似乎导致了这个问题。如果我运行自己的应用程序,我会得到同样的错误,就好像我在上面运行这个:

Traceback (most recent call last):
  File "C:\Anaconda2\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "Calipso.py", line 25, in <module>
  File "C:___.py", line 14, in <module>
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  File "C:\Users\Grant\Documents\GitHub\vocal\calipso\build\exe.win32-2.7\matplotlib\backends\backend_tkagg.py", line 7, in <module>
    from six.moves import tkinter_filedialog as FileDialog
  File "C:\Anaconda2\lib\site-packages\six.py", line 203, in load_module
    mod = mod._resolve()
  File "C:\Anaconda2\lib\site-packages\six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "C:\Anaconda2\lib\site-packages\six.py", line 82, in _import_module
    __import__(name)
ImportError: No module named FileDialog

我的setup.py看起来像是:

import os
import sys
from distutils.core import setup
import cx_Freeze
import matplotlib

base = "Console"

executable = [
    cx_Freeze.Executable("Calipso.py", base = base)
]

build_exe_options = {"includes":["matplotlib.backends.backend_tkagg", "ccplot.algorithms",
                                 "ccplot.hdf", "Tkinter", "tkFileDialog"],
                     "include_files":[(matplotlib.get_data_path(), "mpl-data")],
                     "excludes": ["collections.abc"],
                     }

cx_Freeze.setup(
    name = "py",
    options = {"build_exe": build_exe_options},
    version = "0.0",
    description = "standalone",
    executables = executable
)

如何确保捆绑FileDialog

1 个答案:

答案 0 :(得分:1)

找到了解决此问题的方法。问题是FileDialog是一个独立于Tkinter的包,所以我的脚本现在看起来像:

import os
import sys
from distutils.core import setup
import cx_Freeze
import matplotlib

base = "Console"

executable = [
    cx_Freeze.Executable("Calipso.py", base = base)
]

build_exe_options = {"includes":["matplotlib.backends.backend_tkagg", "ccplot.algorithms",
                                 "ccplot.hdf"],
                     "packages:" ["Tkinter", "tkFileDialog"],
                     "include_files":[(matplotlib.get_data_path(), "mpl-data")],
                     "excludes": ["collections.abc"],
                     }

cx_Freeze.setup(
    name = "py",
    options = {"build_exe": build_exe_options},
    version = "0.0",
    description = "standalone",
    executables = executable
)