For a long time now I couldn't find any help online for my problem. I want to make my own image viewer, since the one that comes with Windows 10 is so bad, but I want to be able to right click on any image, and select open with my Python application. .
Ok, lets try running on an image. First I select 'Choose another app':
Then I selected 'Look for another app on this PC' (test_reg.exe didn't appear the first time) and navigated to the directory in which I had made the executable. The user could select the check box the second time they opened the application to always use my program.
Success! The name of the file was parsed into the application. Now I can create my own GUI to show the image.
Edit: After doing this method with a tkinter application, I kept getting dll errors. This is because cx_Freeze with tkinter requires a couple of dlls, namely tcl86t.dll and tk86t.dll. Normally this is fine because they are in the same directory as the .exe file. But when you run the .exe on an image file. the current working directory (cwd) will be directory in which the image is located, not the directory of the .exe.
I solved this by having the main program a simple python script that changes the cwd to the one in which the one with the dlls in, by using
then running the tkinter app as a module. Then switch back to the original cwd.
The hard part is knowing where the application is installed, which is hard to know if the user installed the app using an msi or something.
Lets take a look at my simple program:
1: import sys
2:
3: try:
4: print(sys.argv[1])
5: except IndexError:
6: print("Being run without file selected.")
7:
8: input("\n\nPress any key to exit...")
I want to just get a simple program working first, i'll build the application later.
Normally argv would be used to parse command line arguments into a program but it turns out that when your program is compiled, when you use 'open with' your program, the second argument will be set to the path to which the file is located. The first argument is the name of the program. So the program prints the second argument. If there isn't one, if the program is just run, then this will throw an IndexError. This is then caught using the try/except block. Then I just do a simple input statement so that the window doesn't just instantly close.
Now I compile this Python script. To do this I use cx_Freeze. Here's the setup script that I used:
I use base = None so that the console isn't removed. Now I run setup.py
The resulting application is really ugly, but it's just a proof of concept. Here's what happens when you run the resulting executable:Now I compile this Python script. To do this I use cx_Freeze. Here's the setup script that I used:
1: from cx_Freeze import setup, Executable
2:
3: setup(
4: name = "test_reg",
5: version = "0.0.2",
6: description = "test_reg",
7: executables = [Executable("test_reg.py", base = None)]
8: )
I use base = None so that the console isn't removed. Now I run setup.py
1: python setup.py build_exe
Ok, lets try running on an image. First I select 'Choose another app':
Then I selected 'Look for another app on this PC' (test_reg.exe didn't appear the first time) and navigated to the directory in which I had made the executable. The user could select the check box the second time they opened the application to always use my program.
Success! The name of the file was parsed into the application. Now I can create my own GUI to show the image.
Edit: After doing this method with a tkinter application, I kept getting dll errors. This is because cx_Freeze with tkinter requires a couple of dlls, namely tcl86t.dll and tk86t.dll. Normally this is fine because they are in the same directory as the .exe file. But when you run the .exe on an image file. the current working directory (cwd) will be directory in which the image is located, not the directory of the .exe.
I solved this by having the main program a simple python script that changes the cwd to the one in which the one with the dlls in, by using
1: os.chdir()
then running the tkinter app as a module. Then switch back to the original cwd.
The hard part is knowing where the application is installed, which is hard to know if the user installed the app using an msi or something.
Comments
Post a Comment