我试图用Python创建一个记事本样式的程序,并且卡在打开文件后单击“保存”即可保存文件而不会向用户显示“另存为FileDialog”的部分。使用下面的代码,由于“ Filename.write(data)”行中的FileSave()函数,导致出现错误“ ValueError:对已关闭文件的I / O操作”。
代码如下:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
global filename
filename = ''
form = Tk()
form.geometry('1000x600')
form.title('Text Editor')
frame = ttk.Frame(form, padding = "3 3 12 12")
frame.grid(column = 0, row = 0, sticky = (N, W, E, S))
frame.columnconfigure(0, weight = 1)
frame.rowconfigure(0, weight = 1)
textentry = Text(frame, width = 95, height = 35)
textentry.grid(column = 0, row = 0, columnspan = 4, sticky = (N, W, E, S))
textentry.focus()
def FileNew():
global filename
textentry.delete('1.0','end')
filename = ''
def FileOpen():
global filename
filename = filedialog.askopenfile(mode = 'r')
if filename != None:
t = filename.read()
textentry.delete('0.0','end')
textentry.insert('0.0', t)
textentry.focus()
filename.close()
def FileSave():
global filename
if filename == '':
filename = filedialog.asksaveasfile(mode = 'w')
if filename != None:
data = textentry.get('1.0','end')
filename.write(data)
filename.close
def FileSaveAs():
global filename
filename = filedialog.asksaveasfile(mode = 'w')
FileSave()
btnNew = ttk.Button(frame, text = 'New', command = FileNew)
btnNew.grid(column = 0, row = 1, sticky = 'S')
btnOpen = ttk.Button(frame, text = 'Open', command = FileOpen)
btnOpen.grid(column = 1, row = 1, sticky = 'S')
btnSave = ttk.Button(frame, text = 'Save', command = FileSave)
btnSave.grid(column = 2, row = 1, sticky = 'S')
btnSaveAs = ttk.Button(frame, text = 'Save As', command = FileSaveAs)
btnSaveAs.grid(column = 3, row = 1, sticky = 'S')
form.mainloop()
答案 0 :(得分:1)
我对您的代码进行了一些编辑,现在一切对我来说似乎正常。现在,您可以打开任何.txt文件并保存对它的更改,而无需调用对话框。我已从您的代码中删除了file.close()
条语句来实现这一点。
filename
将始终等于''
,因此您将在对话框中结束。filename='name of the file that you decided to open'
。 filename.close()
语句会造成麻烦,因为您无法将任何内容保存到关闭的文件中...在这种情况下,如果您将file_open()
函数与filename.close()
语句一起使用在其中,计算机将向您抛出错误,因为您无法将任何内容保存到关闭的文件中。
注意:程序中的保存功能实际上会将所有来自textentry的数据添加到文件中,而不是完全执行保存应做的事情(只是添加新数据)。您可能需要做更多的工作。
我希望这会有所帮助,下面是一些修改的代码:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
filename = ''
form = Tk()
form.geometry('1000x600')
form.title('Text Editor')
frame = ttk.Frame(form, padding="3 3 12 12")
frame.grid(column=0, row=0, sticky=(N, W, E, S))
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
textentry = Text(frame, width=95, height=35)
textentry.grid(column=0, row=0, columnspan=4, sticky=(N, W, E, S))
textentry.focus()
def file_new():
global filename
textentry.delete('1.0', 'end')
filename = ''
def file_open():
global filename
filename = filedialog.askopenfile(mode='r+')
if filename is not None:
t = filename.read()
textentry.delete('0.0', 'end')
textentry.insert('0.0', t)
textentry.focus()
def file_save():
global filename
if filename == '':
filename = filedialog.asksaveasfile(mode='w')
if filename is not None:
data = textentry.get('1.0', 'end')
filename.write(data)
def file_save_as():
global filename
filename = filedialog.asksaveasfile(mode='w')
file_save()
btnNew = ttk.Button(frame, text='New', command=file_new)
btnNew.grid(column=0, row=1, sticky='S')
btnOpen = ttk.Button(frame, text='Open', command=file_open)
btnOpen.grid(column=1, row=1, sticky='S')
btnSave = ttk.Button(frame, text='Save', command=file_save)
btnSave.grid(column=2, row=1, sticky='S')
btnSaveAs = ttk.Button(frame, text='Save As', command=file_save_as)
btnSaveAs.grid(column=3, row=1, sticky='S')
form.mainloop()