【发布时间】:2016-04-20 18:52:03
【问题描述】:
我这几天一直在处理这个问题,希望能得到一些帮助。我开发了一个带有导入模块 tkinter、numpy、scipy、matplotlib 的 GUI 应用程序,它在 python 本身中运行良好。转换为 exe 后,一切都按预期工作,但不是 matplotlib 部分。当我按下我定义的绘图按钮时,exe 只是关闭并且不显示任何绘图。 所以我想做一个最小的例子,我简单地绘制一个 sin 函数,我面临着同样的问题: 在 python 中完美运行,将其转换为 exe 时,按下绘图按钮时会崩溃。这是最小的例子:
import tkinter as tk
import matplotlib.pyplot as plt
import numpy as np
class MainWindow(tk.Frame):
def __init__(self):
tk.Frame.__init__(self,bg='#9C9C9C',relief="flat", bd=10)
self.place(width=x,height=y)
self.create_widgets()
def function(self):
datax = np.arange(-50,50,0.1)
datay = np.sin(datax)
plt.plot(datax,datay)
plt.show()
def create_widgets(self):
plot = tk.Button(self, text='PLOT', command=self.function)
plot.pack()
x,y=120,300
root=tk.Tk()
root.geometry(str(x)+"x"+str(y))
app = MainWindow()
app.mainloop()
这是我对应的setup.py,用于使用 cx_Freeze 进行转换:
import cx_Freeze
import matplotlib
import sys
import numpy
import tkinter
base = None
if sys.platform == "win32":
base = "Win32GUI"
executables = [cx_Freeze.Executable("test.py", base=base)]
build_exe_options = {"includes": ["matplotlib.backends.backend_tkagg","matplotlib.pyplot",
"tkinter.filedialog","numpy"],
"include_files":[(matplotlib.get_data_path(), "mpl-data")],
"excludes":[],
}
cx_Freeze.setup(
name = "test it",
options = {"build_exe": build_exe_options},
version = "1.0",
description = "I test it",
executables = executables)
非常感谢任何可能解决问题的想法。我正在使用 64 位 Windows10 机器,并且正在使用带有 Python 3.4.3 的 WinPython 发行版。
【问题讨论】:
-
想知道这个问题是否与 Windows 10 相关,或者其他 Windows 版本是否也会发生同样的情况。
-
啊,对不起,我忘了提。在 Windows 7 64 位机器上使用相同的 WinPython 发行版也会出现同样的问题。
-
刚在32位XP上试过没问题,我以后在64位win7上试试
-
詹姆斯您好,感谢您提供此信息。很想知道你的 Win 7 系统上会有什么。我也尝试过 Win 8、64 位并遇到同样的问题。
-
命令提示符中应该有和错误代码弹出。尝试从命令提示符打开
exe,这样它就不会立即关闭,让您有时间阅读错误消息。
标签: python python-3.x matplotlib tkinter cx-freeze