I am trying to change the default feather icon in tkinter to a personalized icon. I have the icon file and python file in the same folder. While building the exe using pyinstaller, I use add-data too. I use the following command in command prompt:
pyinstaller --add-data "full_path_and filename_of_icon_file;," -i "full_path_and filename_of_icon_file" scriptname.py
This seems to work because the 'dist' folder contains the .ico file.
If I use the OneFile argument: pyinstaller --onefile --add-data "full_path_and filename_of_icon_file;," -i "full_path_and filename_of_icon_file" scriptname.py
...the dist folder contains just the exe and does not seem to find the .ico file and I get a tkinter.TclError: bitmap "full_path_and filename_of_icon_file" not defined.
In my script, while defining the icon file path, I use os.getcwd + os.path.join to get the current directory of the exe file and the filename of the ico file (assuming that add-data would have added my ico file to the exe). But apparently this is not working.
Can someone please guide me? Ultimately I cannot hardcode the .ico filepath in my script because I don't wish to distribute an exe file plus an .ico file to others; but I just want to distribute the .exe file.
Following is my sample code:
import tkinter as tk
OptionList = [
"Aries",
"Cancer"
]
import sys, os
exe_path = os.getcwd()
ico_file = 'PSI_logo.ico' # or use any .ico file here
ico_path = os.path.join(exe_path, ico_file) # dynamically generating ico path
app = tk.Tk()
app.iconbitmap(ico_path) # dynamically generated path
app.resizable(0,0)
app.geometry('100x200')
variable = tk.StringVar(app)
variable.set("")
opt = tk.OptionMenu(app, variable, *OptionList)
opt.config(width=90, font=('Helvetica', 12))
opt.pack()
def printselection():
print(variable.get())
variable.set("")
mybutton = tk.Button(app, text = 'Click', command = printselection)
mybutton.pack()
app.mainloop()