【发布时间】:2020-07-17 17:19:58
【问题描述】:
我有 anaconda,python 3。我从 Anaconda 启动 cmd.exe 并运行代码:
pyinstaller --onefile guitest4.py。
报告了很多错误,构建了 exe,exe 启动 cmd 窗口并立即关闭。
将代码编译成 exe,包含 tkinter 的代码,工作正常。但是,每当我包含 matplotlib 时,都会出现问题。任何想法?谢谢!
输出:HERE
如果我在 pyinstaller 中添加 -w 选项,我会收到来自 image 的错误
代码:
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import numpy as np
root = tkinter.Tk()
root.wm_title("Embedding in Tk")
fig = Figure(figsize=(5, 4), dpi=100)
t = np.arange(0, 3, .01)
fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
def on_key_press(event):
print("you pressed {}".format(event.key))
key_press_handler(event, canvas, toolbar)
canvas.mpl_connect("key_press_event", on_key_press)
def _quit():
root.quit() # stops mainloop
root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
button = tkinter.Button(master=root, text="Quit", command=_quit)
button.pack(side=tkinter.BOTTOM)
tkinter.mainloop()
# If you put root.destroy() here, it will cause an error if the window is
# closed with the window manager.
【问题讨论】:
-
为什么要从基础环境运行 pyinstaller?这就是它与 Conda 一起使用的方式吗?此外,除非绝对必要,否则请不要将信息作为图像共享。请参阅:meta.stackoverflow.com/questions/303812/…、idownvotedbecau.se/imageofcode、idownvotedbecau.se/imageofanexception。
-
如果不使用 matplotlib,它可以很好地与 conda 一起使用,所以我想是的,它应该可以工作。我有点失望,没有人发布解决方案。
-
从基本环境尝试 pyinstaller 时报告相同。因此,这不是问题。
标签: python matplotlib tkinter pyinstaller