2016-01-24 49 views
0

使用cx_Freeze構建簡單的matplotlib應用程序效果很好,但是當我嘗試從Tkinter & Matplotlib應用程序創建獨立可執行文件時遇到問題。cx_freeze Tkinter&matplotlib後端,沒有名爲FileDialog的模塊

這裏有一個小例子,這將重現錯誤:

#!/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

'「tkinter.filedialog」'也許應該在'「包括」',喜歡這裏的http:/ /stackoverflow.com/questions/34806650/cx-freeze-converted-gui-app-tkinter-crashes-after-presssing-plot-button –

回答

0

找到了解決此問題的方法。問題是,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 
) 
+0

謝謝!這裏有一個我需要做的小修改:而不是''packages:「[」Tkinter「,」tkFileDialog「],'我不得不使用下面的tkinter中的caps和filedialog:''tkinter」,「tkinter.filedialog」 。正如在Python 3.x中寫的[這裏](https://stackoverflow.com/questions/28590669/tkinter-tkfiledialog-doesnt-exist)'tkFileDialog被重命名爲filedialog並放置在Tkinter包中.'。在Python 3.x中,tkinter模塊也沒有上限。 – Alex

相關問題