Search code examples
tkinterpyinstaller

I am trying to wrap up a tkinter GUI by using pyinstaller. In the GUI I am using a . tcl style file but I can't figure out how to bring it along


I have the following example GUI window. I would like to wrap this into an executable for my teammates to use. However, I cannot figure out how to include the "azure.tcl" file into the executable. Without this, it works, but I would like to try and bring it along.

window = tk.Tk()

window.tk.call("source","azure.tcl")

window.mainloop()

I tried the following:

pyinstaller --onefile test.py 

I get the error:

_tkinter.TclError: couldn't read file "azure.tcl": no such file or directory

I know I need to add the .tcl file along with it, but I cannot figure out how, could you please offer any advice?


Solution

  • First you need to include required files into the executable using --add-data option of PyInstaller.

    pyinstaller -F --add-data="azure.tcl:." --add-data="theme:theme" test.py
    

    Then you need to modify your code to load the required file from the temporary directory mentioned in Runtime Information of PyInstaller.

    from pathlib import Path
    ...
    
    # get the directory where the script file is
    progdir = Path(__file__).parent
    
    # function to get the absolute path of a file relative to the script file
    def _path(relpath):
        return progdir / relpath
    
    ...
    
    # load the required file using the above function
    window.tk.call("source", _path("azure.tcl"))
    
    ...