0

I created a text Editor in Python using Tkinter, just a basic one. It has one feature that it can take another argument while opening, for example if in terminal I run python editor.py example.txt my python text editor will open the file "example.txt" in it.

I want any file to open in my text editor when I double click on that file, just like a .txt file opens in notepad when double-clicked, I want to open it in my text editor.

I cannot find any solution to this, please help me.

  • Can you share some Minimal Reproducible Example? Are you saying that when you click on a file in explorer in the IDE, it opens up, which you want to implement –  Jul 17 '21 at 09:58
  • OP wants to open any `.txt` file in OP's editor. OP clearly mentioned that in question. Try read question clearly. And yeah! OP didn't mention what OP tried, and what is found while doing research about it! Try asking that! – imxitiz Jul 17 '21 at 10:01
  • @Kshitiz so that is what I mentioned. Read my comment properly. Also, I know he wants to open text files. My question is how. –  Jul 17 '21 at 10:29
  • What you are saying, IDE, fileexplore I may had been confused that time while reading your comment! And yeah! OP is also asking how? – imxitiz Jul 17 '21 at 10:32
  • A file with a specific extension can be opened in several programs (exe). For example, if I right-click on the TXT file, I see the "Open" option (double-click opens in Notepad), "Open with Sublime Text", "Edit with Notepad++", etc. Don't try to change the default program. Find information on how to add an extra option to the right click menu in Windows regedit. And this question doesn't fit Stack Overflow. – 8349697 Jul 17 '21 at 10:32
  • @8349697 isn't there a folder in windows that allows adding these programs, that is to say that I don't think You need regedit for that. also I think it is possible to do it by going to the option `Open With` and then there is an option to browse files (or "look for another app on this pc" somewhere on the bottom) so then it is possible to add the exe too and I think that way it is also possible to set it as default, so it shouldn't be that hard, still I agree that this doesn't fit well with SO, however if it were about how to automatically do this using Python, then it would be fitting – Matiiss Jul 17 '21 at 11:02
  • @Matiiss "folder in windows that allows adding these programs". Do you mean the Control Panel, where you can set up default programs and file associations? I still think that changing the default Windows programs to open *any file* in a self-made program is not a good idea. – 8349697 Jul 17 '21 at 11:27
  • Take the following code for example, ```import sys import pyttsx3 from os import path engine=pyttsx3.init() try: file=sys.argv[1] except Exception: file=input('Enter file location ') if not path.isfile(file): raise FileNotFoundError with open(file,'r') as f: content=f.read() engine.say(content) engine.runAndWait()``` Please tell me how can see its name in "Open With" list while opening a text file. – Anurag Jain Jul 17 '21 at 11:31
  • @AnuragJain well, that is another issue, but You will have to deal with it, there is no other way to open a non-executable file through that menu tho You can try, otherwise You can add the file browser to Your program using `tkinter.filedialog` options, otherwise You need an exe however that is another issue and I am pretty sure there are solutions You can look up on google – Matiiss Jul 17 '21 at 11:53
  • @8349697 nope, You were right, it has to be done through registry editor (also there is the other option I mentioned about going through the `Open With` menu and selecting the program there) it is just apparently I mixed up some memories making it seem to me that there is just a simple folder You can easily access that allows to add programs to the context menu – Matiiss Jul 17 '21 at 11:57
  • If you don't want to use regedit, you can use this method to [run the Python script via the context menu](https://stackoverflow.com/a/8570432). – 8349697 Jul 17 '21 at 13:43
  • Does [this](https://stackoverflow.com/q/7962157) answer your question? – Henry Jul 17 '21 at 14:21

2 Answers2

0

Ok. This really has nothing much to do with python itself. I have made a similar notepad earlier that you can see on my GitHub page. Anyway, you will have to first convert your tkinter GUI text editor into a .exe file. You can do this by giving the command in the file directory command prompt after installing pyinstaller:

pyinstaller --onefile editor.py

Once this is done, single click on a text file, go to open with on the tools section in you file explorer. You have to then choose open with another app and select you python executable. Now it will work.

Image

0

Inno setup has an option to "associate file type with the main executable"
which you can use with an exe of your program.
how to get "open with"