PyInstaller: Error when executing .exe file from a python file with Sun-valley tkinter theme

huangapple go评论84阅读模式
英文:

PyInstaller: Error when executing .exe file from a python file with Sun-valley tkinter theme

问题

我尝试将名为gui.py的Python文件转换为一个单独的可执行文件,名为number_generator.exe,使用PyInstaller。我使用的是Python 3.11.4和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):
    # ...(此处省略了一些代码)...

if __name__ == "__main__":
    root = Application()
    sv_ttk.set_theme("dark")
    root.mainloop()

我用于打包文件的命令如下:pyinstaller.exe --onefile --windowed --icon=dice.ico --add-data "dice.ico;." -n number_generator --hidden-import=sv-ttk gui.py

然而,在执行number_generator.exe时,我收到了一个错误,其中写着:无法执行脚本'gui',因为未处理的异常:无法读取文件"C:\Users\ben\AppData\Local\Temp\_MEI218962\sv_ttk\sv.tcl",没有这个文件或目录

根据Sun-valley-ttk-theme的Github页面(https://github.com/rdbende/Sun-Valley-ttk-theme),安装过程使用以下pip命令:pip install sv-ttk,我已经执行过这个命令,而且在使用PyInstaller打包之前,脚本运行得非常顺利。

但问题是:你如何解决number_generator.exe文件的执行错误?我已经尝试搜索解决方案,但没有找到一个。非常感谢你的帮助。

英文:

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.

答案1

得分: 1

你是否尝试添加 --collect-data sv_ttk 选项,如此处提到的 链接

英文:

Have you tried adding --collect-data sv_ttk option as mentioned here?

huangapple
  • 本文由 发表于 2023年7月23日 20:44:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定