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

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

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

  1. import tkinter as tk
  2. import sv_ttk
  3. from tkinter import ttk, font, messagebox
  4. from num_gen import number_generator
  5. from check_string_func import check_string
  6. class Application(tk.Tk):
  7. # ...(此处省略了一些代码)...
  8. if __name__ == "__main__":
  9. root = Application()
  10. sv_ttk.set_theme("dark")
  11. 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

  1. import tkinter as tk
  2. import sv_ttk
  3. from tkinter import ttk, font, messagebox
  4. from num_gen import number_generator
  5. from check_string_func import check_string
  6. class Application(tk.Tk):
  7. def __init__(self):
  8. super().__init__()
  9. # Frontend of GUI
  10. self.title("Number Generator")
  11. self.geometry("600x280")
  12. self.iconbitmap("dice.ico")
  13. fontFamilyH1 = font.Font(size = 40)
  14. fontFamilyH2 = font.Font(size = 25)
  15. appLabel = ttk.Label(self, text = "Number Generator", font = fontFamilyH1)
  16. # Input fields
  17. inputFrame = ttk.Frame(self, style = 'styledFrame.TFrame')
  18. self.startValue = tk.StringVar()
  19. startEntry = ttk.Entry(inputFrame, textvariable = self.startValue, width = 5, font = fontFamilyH2)
  20. startLabel = ttk.Label(inputFrame, text = "Start value:", font = fontFamilyH2)
  21. self.endValue = tk.StringVar()
  22. endEntry = ttk.Entry(inputFrame, textvariable = self.endValue, width = 5, font = fontFamilyH2)
  23. endLabel = ttk.Label(inputFrame, text = "End value:", font = fontFamilyH2)
  24. # Generate button
  25. generateButton = ttk.Button(self, text = "Generate", style = 'Accent.TButton', width = 15, command = self.buttonAction)
  26. # Displays widgets to GUI
  27. appLabel.pack(pady = "15")
  28. inputFrame.pack()
  29. startLabel.grid(column = 0, row = 0)
  30. startEntry.grid(column = 1, row = 0, padx = 2)
  31. endLabel.grid(column = 0, row = 1)
  32. endEntry.grid(column = 1, row = 1)
  33. generateButton.pack(pady = "25")
  34. def buttonAction(self):
  35. if not self.startValue.get() or not self.endValue.get(): # Checks if there are any values in startvalue and endvalue
  36. messagebox.showerror(title = "No value(s)", message = "Please input value(s) for start value and end value.")
  37. return
  38. if not check_string(self.startValue.get()) or not check_string(self.endValue.get()): # Checks if start value and end value are integers
  39. messagebox.showerror(title = "Non-integer value(s)", message = "Start value and end value have to be a number, please try again.")
  40. return
  41. if int(self.startValue.get()) >= int(self.endValue.get()): # Checks if start value is greater than end value
  42. messagebox.showerror(title = "Invalid intervall", message = "End value has to be greater than start value, please try again.")
  43. return
  44. numbers = number_generator(int(self.startValue.get()), int(self.endValue.get())) # Initiates when every condition is met
  45. for number in range(len(numbers)):
  46. messagebox.showinfo(title = "Random numbers", message = f"Random number ({number + 1}): {numbers[number]}")
  47. if __name__ == "__main__":
  48. root = Application()
  49. sv_ttk.set_theme("dark")
  50. 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:

确定