1
votes

I have anaconda, python 3. I started cmd.exe from Anaconda and run the code: pyinstaller --onefile guitest4.py. Lots of errors get reported, exe gets built, the exe starts the cmd window and closes immediately. Compiling code into exe, the code containing tkinter, works fine. However, whenever I include matplotlib, there are problems. Any idea? Thanks!

Output: HERE

If I add -w option to pyinstaller, I get the error from the image

The code:

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
Why are you running pyinstaller from the base environment? Is that how it's meant to be used with Conda? Also, please do not share information as images unless absolutely necessary. See: meta.stackoverflow.com/questions/303812/…, idownvotedbecau.se/imageofcode, idownvotedbecau.se/imageofanexception. - AMC
It works with conda just well if no matplotlib is used, so I guess that yes, it is supposed to work. I am kind of disappointed that none has posted a solution. - Aleksandar Ciric
It reports the same when trying pyinstaller from the base environment. Thus that is not the problem. - Aleksandar Ciric

1 Answers

2
votes

The problem is resolved with auto py to exe gui. What it does beyond what I did with pyinstaller I do not know, but it works.