我有一个具有保存和另存为功能的Tkinter程序。我的“另存为”工作正常,但“另存为”却不行。
但是,每当我按下“保存”按钮时,它都会引发错误: NameError:名称“ save_as_dialog”未定义
def save_as():
notepad_save = notepad.get('1.0', END).strip('\n')
head_save = str(head_entry.get().strip('\n')) + '\n'
body_save = str(body_entry.get().strip('\n')) + '\n'
tail_save = str(tail_entry.get().strip('\n')) + '\n'
legs_save = str(legs_entry.get().strip('\n')) + '\n'
save_as_dialog = tkfd.asksaveasfilename(initialfile = file_name,
initialdir = '/Users/Documents', title = "Save as", defaultextension =
'.txt', filetypes = [('Text files', '*.txt')])
with open(save_as_dialog, 'w') as output_file:
output_file.write(head_save)
output_file.write(body_save)
output_file.write(tail_save)
output_file.write(legs_save)
output_file.write(notepad_save)
def save():
notepad_save = notepad.get('1.0', END).strip('\n')
head_save = str(head_entry.get().strip('\n')) + '\n'
body_save = str(body_entry.get().strip('\n')) + '\n'
tail_save = str(tail_entry.get().strip('\n')) + '\n'
legs_save = str(legs_entry.get().strip('\n')) + '\n'
try:
with open(save_as_dialog, 'w') as output_file:
output_file.write(head_save)
output_file.write(body_save)
output_file.write(tail_save)
output_file.write(legs_save)
output_file.write(notepad_save)
except NameErrors:
save_as()
如您所见,文件在不同的行上存储了不同的信息。这是程序的关键部分,因此我需要保持该部分不变。关键是,当我按下保存按钮时,它会引发错误: NameError:名称“ save_as_dialog”未定义。 我尝试过单独使用'except'并使用AttributeError代替,但是它仍然会吐出相同的错误。 我被卡住了!
编辑:我的新代码仍然无法使用:
global save_as_dialog
global opened_file_filename
global filepath
filepath = r"C:\Users\Documents"
opened_file_filename = None
def _open():
opened_file_filename = tkfd.askopenfilename(title = "Open...",
defaultextension = '.txt', filetypes = [('Text files', '*.txt')])
def save_as():
notepad_save = notepad.get('1.0', END).strip('\n') #
head_save = head_entry.get().strip('\n') + '\n'
body_save = body_entry.get().strip('\n') + '\n'
tail_save = tail_entry.get().strip('\n') + '\n'
legs_save = legs_entry.get().strip('\n') + '\n'
save_as_dialog = tkfd.asksaveasfilename(initialfile = file_name, initialdir
= filepath, title = "Save as", defaultextension = '.txt', filetypes =
[('Text files', '*.txt')])
with open(opened_file_filename, 'w') as output_file:
output_file.write(head_save)
output_file.write(body_save)
output_file.write(tail_save)
output_file.write(legs_save)
output_file.write(notepad_save)
opened_file_filename = save_as_dialog
time_saved_label.config(text = "Saved" )
print(opened_file_filename)
def save():
notepad_save = notepad.get('1.0', END).strip('\n')
head_save = head_entry.get().strip('\n') + '\n'
body_save = body_entry.get().strip('\n') + '\n'
tail_save = tail_entry.get().strip('\n') + '\n'
legs_save = legs_entry.get().strip('\n') + '\n'
if opened_file_filename is None:
save_as()
else:
with open(opened_file_filename, 'w') as output_file:
output_file.write(head_save)
output_file.write(body_save)
output_file.write(tail_save)
output_file.write(legs_save)
output_file.write(notepad_save)
time_saved_label.config(text = "Saved" )
print(opened_file_filename)
引发错误: ,其中open(opened_file_filename,'w')作为output_file: FileNotFoundError:[Errno 2]没有这样的文件或目录:''
答案 0 :(得分:2)
那是因为您正在save_as_dialog
内创建save_as()
变量。该变量未在save()
中定义。在这些函数范围之外声明变量应该是最简单的方法。
我将在包含两个函数的范围内做类似save_as_dialog = None
的操作
然后添加
if save_as_dialog is None:
return save_as()
在save()
函数的开头。
结果应如下所示:
from tkinter import filedialog
# Filename of our file.
opened_file_filename = None
def save_as():
"""
Opens a file dialog and saves stuff to it.
:return: None
"""
global opened_file_filename # we wanna use the opened_file_filename from global scope
opened_file_filename = filedialog.askopenfilename()
with(open(opened_file_filename), 'w') as file:
# write stuff to file here
pass
def save():
"""
If we have a filename, save stuff to it. Otherwise do save_as().
:return: None
"""
global opened_file_filename # we wanna use the opened_file_filename from global scope
if opened_file_filename is None:
# it is none, which means we have not chosen a filename yet
return save_as()
with(open(opened_file_filename), 'w') as file:
# write stuff to file here
pass
# rest of your code