pythontkinteropen-with

Opening a text file with your python script


I have created a program that can edit the contents of a text file. Assume that my program looks something like this:

from tkinter import *
from tkinter import filedialog

root = Tk()


def open_file():
    file_to_open = filedialog.askopenfilename(initialdir="C:/" , filetypes=(("All files" , "*.*"),))
    if file_to_open != "":
        file = open(file_to_open , 'r')
        content_in_file = file.read()
        file.close()
        text.delete('1.0' , END)
        text.insert('1.0' , content_in_file)


def save_file():
    path = filedialog.asksaveasfilename(initialdir="C:/" , filetypes=(("All files" , ""),))
    if path != "":
        file = open(path , 'w')
        file.write(text.get('1.0' , 'end-1c'))
        file.close()


text = Text(root , width = 65 , height = 20 , font = "consolas 14")
text.pack()

open_button = Button(root , text = "Open" , command = open_file)
open_button.pack()

save_button = Button(root , text = "Save" , command = save_file)
save_button.pack(pady=20)

mainloop()

The problem here is that when I click on a text file in my file explorer, it opens with the default windows notepad instead of opening with my program.

What I want is that all the text files should open with my program instead of opening with the default windows Notepad.

Here's what I did (in order):

enter image description here

enter image description here

enter image description here

enter image description here

After completing the following steps, I tried opening my text file, but it says:

enter image description here

I tried converting my python program into an exe file (using pyinstaller) and then following the steps above, but when I open the text file I get an other error:

enter image description here

Is there anything wrong with my code or the steps I followed?

I would really appreciate it if anyone could guide me through how I can open a text file with my program.


Solution

  • The code looks fine, it just needed to take the argument, when you open-with you are calling some executable on a path o multiple paths, the first argument is de executable itself, so if the code is executed with more than 1 argument it's a path; the problem with this is that the command is python and not file.py, a fix would be convert it to a exe or call it with a bat.

    This example may look different but is more or less the same, just packed inside a class.

    from tkinter import filedialog
    import tkinter as tk
    import sys
    import os
    
    SRC_PATH = os.path.dirname(__file__)
    
    class File_Editor(tk.Tk):
    
        def __init__(self):
            tk.Tk.__init__(self)
            self.text = tk.Text(self, width=65, height=20, font="consolas 14")
            self.text.pack()
            self.open_button = tk.Button(self, text="Open", command=self.open_file)
            self.open_button.pack()
            self.save_button = tk.Button(self, text="Save", command=self.save_file)
            self.save_button.pack(pady=20)
    
        def open_file(self, file=None):
            path = file or filedialog.askopenfilename(
                initialdir=SRC_PATH, filetypes=(("All files", "*.*"),))
            if os.path.exists(path) and os.path.isfile(path):
                with open(path, 'r', encoding='utf-8') as file:
                    content_in_file = file.read()
                self.text.delete('1.0', tk.END)
                self.text.insert('1.0', content_in_file)
    
        def save_file(self):
            path = filedialog.asksaveasfilename(initialdir=SRC_PATH, filetypes=(("All files", ""),))
            if os.path.exists(path) and os.path.isfile(path):
                with open(path, 'r', encoding='utf-8') as file:
                    file.write(self.text.get('1.0', 'end-1c'))
    
    
    if __name__ == '__main__':
        app = File_Editor()
        if len(sys.argv) > 1:
            app.open_file(sys.argv[1])
        app.mainloop()
    

    This .bat file is just passing the firs argument to the file; a full path is required to execute, it will not be called in a directory you expect.

    @echo off
    python D:\Escritorio\tmp_py\temp_3.py %1
    

    Now you just call the open-with File_Editor.bat