Issue
When I bundle my Tkinter app using pyinstaller --windowed --onefile script.py
on macOS Ventura 14.3, the window containing the matplotlib plot generated from user input does not open. The main Tkinter window shows up fine, but the plot does not open. This only happens on macOS; on Windows, the plot window opens normally. Is there a specific macOS setting, PyInstaller argument, or python code I need to use to display the plot window correctly?
Edit: As requested here is some example code. When using pyinstaller to compile this and clicking on the generate plot button the plot will open.
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("test window")
x_data_var = tk.StringVar()
y_data_var = tk.StringVar()
def create_plot():
x_data_str = x_data_var.get()
y_data_str = y_data_var.get()
x_data = [float(i) for i in x_data_str.split(',')]
y_data = [float(i) for i in y_data_str.split(',')]
plt.plot(x_data, y_data)
plt.show()
ttk.Label(root, text='X Data: ').grid(row=0, column=0, padx=5, pady=5)
ttk.Label(root, text='Y Data: ').grid(row=1, column=0, padx=5, pady=5)
ttk.Entry(root, textvariable=x_data_var).grid(row=0, column=1, padx=5, pady=5)
ttk.Entry(root, textvariable=y_data_var).grid(row=1, column=1, padx=5, pady=5)
ttk.Button(root, text="Create Plot", command=create_plot).grid(row=2, column=0, columnspan=2, padx=5, pady=5)
root.mainloop()
Solution
After doing some further investigation, and looking at the output from pyinstaller, when compiling the code I found the problem. When compiling, for some reason matplotlib was not being 'collected'. I was able to fix this by adding the --collect-all *package*
to the pyinstaller command:
pyinstaller --collect-all matplotlib --onefile --windowed script.py
Explanation:
- --collect-all package_name: Instructs PyInstaller to gather all files from the specified package
- --onefile: Creates a single executable file
- --windowed: Specifies that the app has a graphical interface
- script.py: Script name
Answered By - Mitchell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.