cx_freeze 使用 Tkinter 和 matplotlib 后端,没有名为 FileDialog 的模块

cx_freeze with Tkinter & matplotlib backend, No module named FileDialog

使用 cx_Freeze 构建简单的 matplotlib 应用程序效果很好,但是我 运行 在尝试从 Tkinter 和 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

已找到解决此问题的方法。问题是 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
)

编辑:修复了引号外的冒号。