Tom Wright Tom Wright - 6 months ago 17
Python Question

Opening custom file types in cxFreeze complied python executable on opening file

I am using cx_Freeze to compile a Rubiks Cube Simulator in python; which uses tkinter.

Program Screenshot

I would like the user to be able to save the layout of the 2d representation you can see in the centre, into .cube files, and be able to open previous .cube files from the program itself.

However, I also want the user to be able to open .cube files from explorers and have the program startup displaying the contents of the .cube file that the user opened.

Having done some research, I think I need to access the "Runtime Environment" or something - but otherwise I have absolutely no idea.

Answer

UPDATE

I solved this using the argparse module. Based on the fact that every time explorer opens a file it calls the application with the argument of the file directory, all I had to do was add an optional parameter to catch this data.

import argparse
parser=argparse.ArgumentParser()
parser.add_argument("cubefile",nargs="?",default=False)
#'nargs="?"' makes the argument optional
#-meaning an error will not be thrown if no file is parsed on execution
args=parser.parse_args()
if args.universefile != False:
    init_defaultcube = cubetools.getCubeFromCubeFileDir(args.universefile)
    #cubetools is my class and getCubeFromCubeFileDir just interprets the text in the file

However, because this argument changed the working directory of the exe, and my GUI image references were relative, I had to reset the current directory using
os.chdir(os.path.dirname(os.path.abspath(sys.executable)))
I'm now working on modifying the registry on initialisation to set the default app and icon of .cube files.