如何在我用python(tkinter)创建的texteditor中打开文件

2024-10-01 05:05:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用以下代码在Sublime text 3中使用python(tkinter)制作一个简单的文本编辑器:

from tkinter import *
from tkinter import filedialog

root=Tk()

def OpenFile():
    file_name=filedialog.askopenfilename(initialdir="/Desktop\python", title="Select a File:", filetype=(("Txt Files",".txt"),("All Files","*.*")))
    content = open(file_name).read()
    txteditor.insert(END, content)
    
def SaveFile():
    myfile = filedialog.asksaveasfile(mode='w', defaultextension='.txt')
    if myfile is None:
        return
    content=txteditor.get(1.0,'end-1c')
    myfile.write(content)

root.iconbitmap(default='icon2.ico')
root.title('My Notepad')
txteditor=Text(root, width=50, height=20).pack()

openbtn=Button(root, text='Open', command=OpenFile)
openbtn.pack()

savebtn=Button(root, text='Open', command=OpenFile)
savebtn.pack()

但是,当我单击“打开”按钮时,窗口会出现,但当我单击“打开”按钮时(在选择要打开的文件后),它在第10 error I am getting when I run my code with Sublime Text 3 IDE(see in the image)行中给出了错误


Tags: textnamefromimporttitletkinterdefroot
1条回答
网友
1楼 · 发布于 2024-10-01 05:05:48

问题在于\。python将其解释为转义斜杠-类似于用于\n交换单\\\的转义斜杠。我知道您提供的错误,您可以看到\和大写字母。口译员试图避开突出显示的字母

相关问题 更多 >