I have tried to convert a python file called gui.py
, shown below, to a single, executable file called number_generator.exe
using PyInstaller. I have used Python 3.11.4 and PyInstaller 5.13.0.
gui.py
import tkinter as tk
import sv_ttk
from tkinter import ttk, font, messagebox
from num_gen import number_generator
from check_string_func import check_string
class Application(tk.Tk):
def __init__(self):
super().__init__()
# Frontend of GUI
self.title("Number Generator")
self.geometry("600x280")
self.iconbitmap("dice.ico")
fontFamilyH1 = font.Font(size = 40)
fontFamilyH2 = font.Font(size = 25)
appLabel = ttk.Label(self, text = "Number Generator", font = fontFamilyH1)
# Input fields
inputFrame = ttk.Frame(self, style = 'styledFrame.TFrame')
self.startValue = tk.StringVar()
startEntry = ttk.Entry(inputFrame, textvariable = self.startValue, width = 5, font = fontFamilyH2)
startLabel = ttk.Label(inputFrame, text = "Start value:", font = fontFamilyH2)
self.endValue = tk.StringVar()
endEntry = ttk.Entry(inputFrame, textvariable = self.endValue, width = 5, font = fontFamilyH2)
endLabel = ttk.Label(inputFrame, text = "End value:", font = fontFamilyH2)
# Generate button
generateButton = ttk.Button(self, text = "Generate", style = 'Accent.TButton', width = 15, command = self.buttonAction)
# Displays widgets to GUI
appLabel.pack(pady = "15")
inputFrame.pack()
startLabel.grid(column = 0, row = 0)
startEntry.grid(column = 1, row = 0, padx = 2)
endLabel.grid(column = 0, row = 1)
endEntry.grid(column = 1, row = 1)
generateButton.pack(pady = "25")
def buttonAction(self):
if not self.startValue.get() or not self.endValue.get(): # Checks if there are any values in startvalue and endvalue
messagebox.showerror(title = "No value(s)", message = "Please input value(s) for start value and end value.")
return
if not check_string(self.startValue.get()) or not check_string(self.endValue.get()): # Checks if start value and end value are integers
messagebox.showerror(title = "Non-integer value(s)", message = "Start value and end value have to be a number, please try again.")
return
if int(self.startValue.get()) >= int(self.endValue.get()): # Checks if start value is greater than end value
messagebox.showerror(title = "Invalid intervall", message = "End value has to be greater than start value, please try again.")
return
numbers = number_generator(int(self.startValue.get()), int(self.endValue.get())) # Initiates when every condition is met
for number in range(len(numbers)):
messagebox.showinfo(title = "Random numbers", message = f"Random number ({number + 1}): {numbers[number]}")
if __name__ == "__main__":
root = Application()
sv_ttk.set_theme("dark")
root.mainloop()
The command I used for packaging of the file was the following: pyinstaller.exe --onefile --windowed --icon=dice.ico --add-data "dice.ico;." -n number_generator --hidden-import=sv-ttk gui.py
However, upon execution of number_generator.exe
, I receive an error that states: Failed to execute script 'gui' due to unhandled exception: couldn't read file "C:\Users\ben\AppData\Local\Temp\_MEI218962\sv_ttk\sv.tcl" no such file or directory
According to the Github page for the Sun-valley-ttk-theme (https://github.com/rdbende/Sun-Valley-ttk-theme), the installation process uses the following pip command: pip install sv-ttk
which I have done and the script worked flawlessly before packaging with PyInstaller.
But the question is: how do you solve the execution error fron the number_generator.exe
file? I have tried searching for a solution but haven't found one. I would be very grateful for your help.
Have you tried adding --collect-data sv_ttk
option as mentioned here?