使用python脚本打开文本文件

使用python脚本打开文本文件,python,tkinter,open-with,Python,Tkinter,Open With,我创建了一个可以编辑文本文件内容的程序。假设我的程序如下所示: from tkinter import * from tkinter import filedialog root = Tk() def open_file(): file_to_open = filedialog.askopenfilename(initialdir="C:/" , filetypes=(("All files" , "*.*"),))

我创建了一个可以编辑文本文件内容的程序。假设我的程序如下所示:

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()
这里的问题是,当我在文件资源管理器中单击一个文本文件时,它会用默认的windows记事本打开,而不是用我的程序打开

我想要的是,所有的文本文件都应该用我的程序打开,而不是用默认的windows记事本打开

以下是我所做的(为了):

完成以下步骤后,我尝试打开文本文件,但它显示:

我尝试将python程序转换为
exe
文件(使用
pyinstaller
),但随后出现了另一个错误:

我的代码或我遵循的步骤有什么问题吗


如果有人能指导我如何用我的程序打开一个文本文件,我将不胜感激。

代码看起来很好,只需要取参数,当你用
打开时,你正在调用一个路径上的某个可执行文件o多个路径,第一个参数本身就是反可执行文件,因此,如果代码使用超过1个参数执行,则它是一条路径;问题是,该命令是
python
而不是
file.py
,修复方法是将其转换为exe或使用bat调用

这个例子看起来可能不同,但大致相同,只是打包在一个类中

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()
这个.bat文件只是将firs参数传递给该文件;执行时需要完整路径,在您期望的目录中不会调用该路径

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

现在您只需调用
openwithfile_Editor.bat

就可以知道,当您用程序打开一个文件时,窗口会作为参数传入程序。你从不处理这场争论。此外,您不能将python脚本设置为默认应用程序,因为它不是可执行文件。你能详细介绍一下你是如何把它编译成一个
.exe
的吗?为什么你要把初始目录设置为
SRC\u PATH
-几乎总是把它添加到文件管理脚本中,当它是一个命令行实用程序时,比如参数有一个默认值,或者在测试一个模块时给它一个不同的执行参数(与执行保护相结合),但是它的所有功能都是preference@Alex:我实际上没有使用批处理脚本,那么,您能解释一下批处理文件中的这两行是做什么的吗?
@echo off
表示不在控制台中显示消息,第二行只是一个命令,就像在终端中一样,但在批处理中您有
%*
值,这些平均参数,
sys.argv[1]
%1
相同;如果您安装了.py,则不再需要.bat文件。