pyinstaller不能与tkinter和matplotlib一起使用

时间:2020-04-05 18:27:23

标签: python matplotlib tkinter pyinstaller

我有蟒蛇蟒蛇3。我从蟒蛇启动了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.

1 个答案:

答案 0 :(得分:2)

通过自动py到exe gui解决了该问题。除了我不知道的pyinstaller所做的以外,它还能做什么,但是它可以工作。