【问题标题】:cx_freeze with Tkinter & matplotlib backend, No module named FileDialog带有 Tkinter 和 matplotlib 后端的 cx_freeze,没有名为 FileDialog 的模块
【发布时间】:2016-04-30 23:28:19
【问题描述】:

使用 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

【问题讨论】:

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


【解决方案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
)

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

【讨论】:

  • 谢谢!这是我需要做的一个小修改:而不是"packages:" ["Tkinter", "tkFileDialog"],,我不得不使用tkinter下面的大写和文件对话框:"tkinter", "tkinter.filedialog"。正如 Python 3.x tkFileDialog was renamed to filedialog and placed inside the Tkinter package. 中写的 here。在 Python 3.x 中,tkinter 模块也没有大写字母。
  • 您好,将 ["Tkinter", "tkFileDialog"] 添加到包中是否足以解决问题?我有同样的问题,但这没有帮助。使用 Python 2.7.3 和 cx_Freeze 5.1.1
猜你喜欢
  • 1970-01-01
  • 2016-07-19
  • 1970-01-01
  • 2014-11-11
  • 2017-08-15
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多